【问题标题】:How to get firestore data in another widget如何在另一个小部件中获取 Firestore 数据
【发布时间】:2020-10-24 14:36:21
【问题描述】:

到目前为止,该应用可以做什么: 到目前为止,我有一个应用可以在 Google 地图上显示从 Firestore 获取坐标的标记。

我想要什么:我想要当用户按下标记时,他会来到另一个屏幕,在该屏幕上显示更多数据(数据也在 firestore 上)。

问题:我不知道如何获取要显示详细信息的屏幕上的数据(例如姓名)。

我有一个 onTap 函数,它调用goToDetailPage()

这就是 goToDetailPage() 函数:

void goToDetailPage() {
    Navigator.of(context).push(new MaterialPageRoute(
        builder: (BuildContext context) => new detailPage()));
  }

这是“绘制”标记的代码:

 void initMarker(specify, specifyId) async {
    var markerIdVal = specifyId;
    final MarkerId markerId = MarkerId(markerIdVal);
    final Marker marker = Marker(
        markerId: markerId,
        position:
            LatLng(specify['location'].latitude, specify['location'].longitude),
        infoWindow: InfoWindow(title: specify['name'], snippet: 'Shop'),
        onTap: () {
          goToDetailPage();
        });
    print(specify['location'].latitude);
    nameTest = specify['name'];
    setState(() {
      markers[markerId] = marker;
      print(markerId.toString() + '__________________________');
    });
  }

这是详情页的代码:

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Detail Page'),
      ),
      body: Column(children: <Widget>[Text(specify['name'])]),
    );

问题是详细页面中的指定['name']中的指定带有红色下划线,我不知道这是什么原因。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore android-context


    【解决方案1】:

    在第二页:

    class Tester extends StatefulWidget {
    
      String dataFromLastPage;
      Tester({Key key, this.dataFromLastPage}) : super(key: key);
    
      @override
      _TesterState createState() => _TesterState();
    }
    
    class _TesterState extends State<Tester> {
      @override
      Widget build(BuildContext context) {
        return Text(widget.dataFromLastPage); //"thisWillGoToTheNextPage!"
      }
    }
    

    导航到该页面时,发送如下数据:

    Navigator.of(context).push(new MaterialPageRoute(
            builder: (BuildContext context) => new Tester(dataFromLastPage: 'thisWillGoToTheNextPage!')));
    

    【讨论】:

      【解决方案2】:

      尝试按照文档所说的去做:https://firebase.flutter.dev/docs/firestore/usage/#realtime-changes

      但是,文档中没有指定的几件事

      • 要首先使用 firestore,您需要初始化 firestore 应用,为此您只需在 dart 文件的 main 方法中编写一段特定的代码
      void main() async{
        WidgetsFlutterBinding.ensureInitialized();
        await Firebase.initializeApp();
        runApp(MyApp());
      }
      
      • 要连接到您的 Firestore 数据库,只需输入您的小部件构建函数

        CollectionReference &lt;Variable name&gt; = FirebaseFirestore.instance.collection(&lt;Name of collection as string&gt;);

      • 现在,如果您正在创建一个列表,您可以简单地使用 StreamBuilder 函数并通过(初始化文档中给出的快照)获取文档中的数据

      snapshot.data.docs[index].data()[<Identifier of your document such as
       'Name' or 'location'>]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 2021-04-03
        • 2020-08-20
        • 1970-01-01
        相关资源
        最近更新 更多