首页 > 解决方案 > 如何在颤动的另一个页面上从地图中获取价值

问题描述

所以我有一个列表视图,我使用 Future 来获取数据,它显示得很好。现在我正在尝试将单击项目上的值从列表视图页面解析到另一个页面,该页面将显示项目单击的详细信息。请问我该如何实现?

未来

List dealData = List();
  Future<String> _fetchComment() async {
    setState(() {
      isLoading = true;
      debugPrint("emirate state");
    });
    try {
      debugPrint("emirate try");

      final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        print('connected');
        debugPrint("emirate connect");

        String url;

        debugPrint("my select:$_mySelection");

        if (_mySelection == null && _myFeatureSelection == null) {
          url = "my rest api";
          
        } else if (_myFeatureSelection != null) {
          url =
              "my rest api";
          _mySelection = null;
        } else if (_mySelection != null && _myFeatureSelection == null) {
          url = "my rest api";

        }

        print("our url:$url");

        var res = await http
            .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
        var resBody = json.decode(res.body);

        debugPrint("emirate url:$url");

        setState(() {
          dealData = resBody;
          isLoading = false;
        });

        print(resBody);
        debugPrint("emirate:$resBody");

        return "Sucess";
      } else {
        throw Exception('Failed to load profile');
      }
    } on SocketException catch (_) {
      print('not connected');
      setState(() => isLoading = false);

      Navigator.popUntil(
          context, (_) => !Navigator.canPop(context));


      Navigator.pushReplacement(
          context,
          new MaterialPageRoute(
              builder: (BuildContext context) => NoInternet()));


    }
  }

我的列表视图和 onclick


dealData
         .map(
   
           (position) => FutureBuilder<String>(
               future: getDistance(
                   position["lat"],
                   position["lng"])
                   .then((value) =>
                   value.toString()),

       builder: (context, snapshot) {


         double myrate = double.parse(
             position["ratings"] ==
                 null
                 ? "0"
                 : position["ratings"]);

         return Container(
             child:Card(child:
             GestureDetector(
                 onTap: () {

                   print(position); // position printed here

                   Navigator.push(
                       context,
                       MaterialPageRoute(
                           builder: (BuildContext ctx) => Maps(position)));


                 },
     ).toList(),

我的地图课

class mapsFinal extends StatefulWidget {
 final int position;

 const mapsFinal(this.position);

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

class _MapsState extends State<mapsFinal> {
 Widget build(BuildContext context) {
   return Text("title" + widget.position.toString());
 }
}



请我需要第二页来显示我在此处单击的项目。

标签: flutterlistviewhashmap

解决方案


这是将值传递给名为“Maps”的小部件的最简单示例:

// BOILERPLATE CODE TO MAKE THE EXAMPLE RUN

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Maps("THE VALUE"),
        ),
      ),
    );
  }
}

// THIS IS THE CLASS YOU NEED TO LOOK AT:

class Maps extends StatefulWidget {
  final String position;
  
  const Maps(this.position);

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

class _MapsState extends State<Maps> {
  Widget build(BuildContext context) {
    return Text("You passed: " + widget.position);
  }
}

推荐阅读