0

我是 Flutter 世界和移动应用程序开发的新手,并且正在为如何在我的应用程序中传递数据而苦苦挣扎。这是我的代码,如何在同一页面上将数据从未来传递到另一个未来?这些是我的两个未来,我的班级和我的主页,请在未来建设者的帮助下。
这是我返回位置的未来。从她那里,我需要将位置 ID 传递给下一个未来。

Future<Location> Lastlocation() async {
    final prefs = await SharedPreferences.getInstance();
    final key = 'token';
    final value = prefs.get(key) ?? 0;
    String myUrl = "$serverUrl/location/getlastlocation?token=" + value;
    http.Response response = await http.get(
      myUrl,
      headers: {
        'Accept': 'application/json',
        //'Authorization': 'token $value'
      },
    );
    if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      return Location.fromJson(json.decode(response.body));
    } else {
      // then throw an exception.
      throw Exception('Failed to load album');
    }
  }

**这是第二个未来,它返回取决于第一个未来的位置 ID 的天气列表 * Lastlocation()

 Future<List> Getweither(String ID) async {
    final prefs = await SharedPreferences.getInstance();
    final key = 'token';
    final value = prefs.get(key) ?? 0;

    String myUrl = "$serverUrl/dashbaord/Getweither/$ID?token=" + value;
    http.Response response = await http.get(myUrl,
        headers: {
          'Accept': 'application/json',
        });
    print("myUrldevice :"+myUrl);
    print("status :"+response.statusCode.toString());

    return json.decode(response.body);
  }

这是我的课堂位置

// To parse this JSON data, do
//
//     final location = locationFromJson(jsonString);

import 'dart:convert';

List<Location> locationFromJson(String str) => List<Location>.from(json.decode(str).map((x) => Location.fromJson(x)));

String locationToJson(List<Location> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Location {
  Location({
    this.automaticIrrigation,
    this.coordinates,
    this.createdDate,
    this.sensorIds,
    this.id,
    this.siteName,
    this.description,
    this.v,
  });

  bool automaticIrrigation;
  List<double> coordinates;
  DateTime createdDate;
  List<String> sensorIds;
  String id;
  String siteName;
  String description;
  int v;

  factory Location.fromJson(Map<String, dynamic> json) => Location(
    automaticIrrigation: json["AutomaticIrrigation"],
    coordinates: List<double>.from(json["Coordinates"].map((x) => x.toDouble())),
    createdDate: DateTime.parse(json["Created_date"]),
    sensorIds: List<String>.from(json["Sensor_ids"].map((x) => x)),
    id: json["_id"],
    siteName: json["SiteName"],
    description: json["Description"],
    v: json["__v"],
  );

  Map<String, dynamic> toJson() => {
    "AutomaticIrrigation": automaticIrrigation,
    "Coordinates": List<dynamic>.from(coordinates.map((x) => x)),
    "Created_date": createdDate.toIso8601String(),
    "Sensor_ids": List<dynamic>.from(sensorIds.map((x) => x)),
    "_id": id,
    "SiteName": siteName,
    "Description": description,
    "__v": v,
  };
}

这是我的主页

import 'dart:convert';
import 'dart:developer';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sidebar_animation/Services/DataHelpers.dart';
import 'package:sidebar_animation/sidebar/sidebar_layout.dart';
import '../bloc.navigation_bloc/navigation_bloc.dart';
import 'package:sidebar_animation/constants.dart';
import 'package:flutter/gestures.dart';
import 'package:sidebar_animation/bloc.navigation_bloc/navigation_bloc.dart';

class HomePage extends StatelessWidget with NavigationStates {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {


  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
   DatabaseHelper2 databaseHelper2 = new DatabaseHelper2();
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      body: ListView(
          FutureBuilder(
              future: databaseHelper2.Getweither(location_id),
              builder: (context,snapshot) {
                if (snapshot.hasError)
                {
                  print(snapshot.error);
                  print("there is problem !");
                }
                return snapshot.hasData
                    ?  ItemList(list: snapshot.data)
                    :  Center(child: CircularProgressIndicator(
                ),
                );
              }
),
    );
  }

  Widget _buildProgrammCard() {
    return Container(
        height: 90,
       child:
            Card(
              semanticContainer: true,
              clipBehavior: Clip.antiAliasWithSaveLayer,
              color: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              elevation: 4,
              margin: EdgeInsets.fromLTRB(14, 0, 14, 14),
              child:
              FutureBuilder(
                  future: databaseHelper2.Lastlocation(),
                  builder: (context,snapshot) {
                    if (snapshot.hasError)
                    {
                      print(snapshot.error);
                      print("mochkla lenaa *");
                    }
                    return snapshot.hasData
                        ?  Text("Location :" +snapshot.data.siteName)
                        :  Center(child: CircularProgressIndicator(
                    ),
                    );
                  }
              ),
            ),

    );

  }

class ItemList extends StatelessWidget{
  List list;
  ItemList({this.list});

  ScrollController _controller = new ScrollController();


  @override
  Widget build(BuildContext context) {
    return  ListView.builder(
      itemCount: list == null ? 0 : list.length,
      scrollDirection: Axis.horizontal,
      itemExtent: 190.0,
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.fromLTRB(10, 0, 0, 14),
          child: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: NetworkImage(
                  item.storyUrl,
                ),
                fit: BoxFit.cover,
                colorFilter: ColorFilter.mode(
                  Colors.black26,
                  BlendMode.darken,
                ),
              ),
              borderRadius: BorderRadius.circular(10.0),
              color: Colors.grey,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: Text(
                    "temp :",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(left: 24),
                  child:
                  Text(
                    list[i]['Weither']['Temp'],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    ),

  }
}

最后,我需要从第一个 future 中获取 location id,它将 Location 返回到第二个 future Getweither(String ID)以获取指定位置的天气。

4

1 回答 1

0

只需嵌套两个 FutureBuilder,并将其snapshot.data作为第二个的参数传递。

简约示例:

FutureBuilder(
  future: databaseHelper2.Lastlocation(),
  builder: (context, snapshot) {
    if (snapshot.hasError) {
      print(snapshot.error);
      print("there is problem !");
    }
   
    return snapshot.hasData
                    ?  FutureBuilder(
                         future: databaseHelper2.Getweither(snapshot.data.id),
                         builder: (context, snapshot2) {...})
                    :  Center(child: CircularProgressIndicator());
  } 
);
于 2021-01-08T14:49:15.117 回答