首页 文章

颤动网格视图值不更新

提问于
浏览
0

在我的flutter应用程序中,我使用带有firebase的GridView . 来自firebase的值正在增加 . 写入在状态类initState中检索的firebase值 . 我声明了一个变量 document . 我想要 createchildwidget() 中的详细信息值 . 但在 createchildwidget() 显示为null . 如何在 createchildwidget() 函数中检索 dertails . 请帮我 .

import 'package:flutter/material.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';

  void main() => runApp(MyApp());

  class MyApp extends StatelessWidget {
    // This widget is the root of your application.

    MyApp({this.firestore});
    final Firestore firestore;


    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          home: MyHomePage(title: 'My Shop', firestore: firestore)
      );
    }
  }

  class MyHomePage extends StatefulWidget {

    MyHomePage({Key key, this.title, this.firestore}) : super(key: key);
    final Firestore firestore;
    final String title;

    @override
    _MyHomePageState createState() => new _MyHomePageState(firestore: firestore);
  }

  class _MyHomePageState extends State<MyHomePage> {
    _MyHomePageState({this.firestore});
    final Firestore firestore;
    var details = [];

    void initState(){
      super.initState();
      Firestore.instance.collection("names").getDocuments().then((data) async{
        var list = data.documents;
        details = list;
        print("init state document:"+details.length.toString());  // value is getting

        setState((){
          details = list;
        });
      });



    }

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          iconTheme: IconThemeData(
            color: Colors.black, //change font color here
          ),
          backgroundColor: new Color(0xFFFAFAFA),
        )
        title:"title",
        body: TheGridview().build(),
      );
    }
  }

  class GridviewClass extends _MyHomePageState{
    Card addGridCell(String name, IconData icon){
      return Card(
        elevation: 1.0,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisSize: MainAxisSize.min,
          verticalDirection: VerticalDirection.down,
          children: <Widget>[
            Center(child: Icon(icon)),
            Text(name)
          ],
        ),
      );
    }

    @override
    Widget build(BuildContext context) {
      return GridView.count(
        primary: true,
        padding: EdgeInsets.all(1.0),
        crossAxisCount: 2,
        childAspectRatio: 1.0,
        mainAxisSpacing: 1.0,
        crossAxisSpacing: 1.0,

        children: createchildwidget(),

        /*  children: <Widget>[
              addGridCell("Home", Icons.access_alarm)
            ],*/


      );
    }

    List<Widget> createchildwidget(){
      print("createchildwidget:"+details.length.toString()); // the value getting 0
      List<Widget> createchildwidget = List<Widget>();
      for(int i=0;i<details.length;i++){
        createchildwidget.add(GridviewClass().addGridCell(details[i].data['message'], Icons.access_alarm));

        }
            return createchildwidget;
        }


  }

1 回答

  • 0

    我从下面的代码解决了这个问题 . 我将类 createchildwidget 更改为函数并写入 _MyHomePageState

    import 'dart:async';
      import 'package:flutter/material.dart';
      import 'package:augr/location/LocationScreen.dart';
      import 'package:cloud_firestore/cloud_firestore.dart';
    
      void main() => runApp(MyApp());
    
      class MyApp extends StatelessWidget {
        // This widget is the root of your application.
    
        MyApp({this.firestore});
        final Firestore firestore;
    
    
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
              home: MyHomePage(title: 'My Shop', firestore: firestore)
          );
        }
      }
    
      class MyHomePage extends StatefulWidget {
    
        MyHomePage({Key key, this.title, this.firestore}) : super(key: key);
        final Firestore firestore;
        final String title;
    
        @override
        _MyHomePageState createState() => new _MyHomePageState(firestore: firestore);
      }
    
      class _MyHomePageState extends State<MyHomePage> {
        _MyHomePageState({this.firestore});
        final Firestore firestore;
    
        var documents = [];
        bool isDocLoaded=false;
    
    
        void initState() {
          Firestore.instance.collection("messages").getDocuments().then((data) async {
            var list = data.documents;
            documents = list;
            print("init state document:" +
                documents.length.toString()); // value is getting
            super.initState();
            setState(() {
              isDocLoaded = true;
              documents = list;
            });
          });
        }
    
        @override
        Widget build(BuildContext context) {
          return new Scaffold(
            appBar: new AppBar(
              iconTheme: IconThemeData(
                color: Colors.black, //change font color here
              ),
              backgroundColor: new Color(0xFFFAFAFA),
            )
            title:"title",
            body: isDocLoaded? TheGridview():Center(child:CircularProgressIndicator()),
          );
        }
    
        Widget TheGridView(){
          return GridView.count(
            primary: true,
            padding: EdgeInsets.all(1.0),
            crossAxisCount: 2,
            childAspectRatio: 1.0,
            mainAxisSpacing: 1.0,
            crossAxisSpacing: 1.0,
    
            children: createChildrenTexts(),
    
            /*  children: <Widget>[
                  makeGridCell("Home", Icons.access_alarm)
                ],*/
          );
        }
    
        Card makeGridCell(String name, IconData icon){
          return Card(
            elevation: 1.0,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              mainAxisSize: MainAxisSize.min,
              verticalDirection: VerticalDirection.down,
              children: <Widget>[
                Center(child: Icon(icon)),
                Text(name)
              ],
            ),
          );
        }
        List<Widget> createChildrenTexts(){
          print("createChildrenTexts:"+documents.length.toString()); // the value getting 0
          List<Widget> childrenTexts = List<Widget>();
          for(int i=0;i<documents.length;i++){
            childrenTexts.add(makeGridCell(makeGridCell(data[i].data['message'], Icons.access_alarm));
            }
                return createchildwidget;
            }
      }
    

相关问题