flutter initstate() list is not accessing another class widget












1















I am creating an app with flutter. I want to fetch data from firebase and and shows grid view. The data is fetching correctly from firebase. The data fetching in initState() But it is not updating the another class TheGridview extends from _MyHomePageState. I am using a variable
documents for storing list from firebase. initState() documents value but in the method createchildwidget the document became null. I tried so many methods please help me. The code is shown below.



    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 = ;

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((){
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: TheGridview().build(),
);
}
}

class TheGridview extends _MyHomePageState{
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)
],
),
);
}

@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>[
makeGridCell("Home", Icons.access_alarm)
],*/


);
}

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

}
return createchildwidget;
}


}









share|improve this question





























    1















    I am creating an app with flutter. I want to fetch data from firebase and and shows grid view. The data is fetching correctly from firebase. The data fetching in initState() But it is not updating the another class TheGridview extends from _MyHomePageState. I am using a variable
    documents for storing list from firebase. initState() documents value but in the method createchildwidget the document became null. I tried so many methods please help me. The code is shown below.



        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 = ;

    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((){
    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: TheGridview().build(),
    );
    }
    }

    class TheGridview extends _MyHomePageState{
    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)
    ],
    ),
    );
    }

    @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>[
    makeGridCell("Home", Icons.access_alarm)
    ],*/


    );
    }

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

    }
    return createchildwidget;
    }


    }









    share|improve this question



























      1












      1








      1








      I am creating an app with flutter. I want to fetch data from firebase and and shows grid view. The data is fetching correctly from firebase. The data fetching in initState() But it is not updating the another class TheGridview extends from _MyHomePageState. I am using a variable
      documents for storing list from firebase. initState() documents value but in the method createchildwidget the document became null. I tried so many methods please help me. The code is shown below.



          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 = ;

      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((){
      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: TheGridview().build(),
      );
      }
      }

      class TheGridview extends _MyHomePageState{
      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)
      ],
      ),
      );
      }

      @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>[
      makeGridCell("Home", Icons.access_alarm)
      ],*/


      );
      }

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

      }
      return createchildwidget;
      }


      }









      share|improve this question
















      I am creating an app with flutter. I want to fetch data from firebase and and shows grid view. The data is fetching correctly from firebase. The data fetching in initState() But it is not updating the another class TheGridview extends from _MyHomePageState. I am using a variable
      documents for storing list from firebase. initState() documents value but in the method createchildwidget the document became null. I tried so many methods please help me. The code is shown below.



          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 = ;

      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((){
      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: TheGridview().build(),
      );
      }
      }

      class TheGridview extends _MyHomePageState{
      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)
      ],
      ),
      );
      }

      @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>[
      makeGridCell("Home", Icons.access_alarm)
      ],*/


      );
      }

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

      }
      return createchildwidget;
      }


      }






      android ios dart flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 7:22







      Joe

















      asked Nov 25 '18 at 19:16









      JoeJoe

      569




      569
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Instead of making TheGridView as a new class, make it as a function and call it when document data is loaded.
          Try this:



              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 _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: createchildwidget(),

          /* 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> createchildwidget(){
          print("createchildwidget:"+documents.length.toString()); // the value getting 0
          List<Widget> createchildwidget = List<Widget>();
          for(int i=0;i<documents.length;i++){
          createchildwidget.add(makeGridCell(makeGridCell(data[i].data['message'], Icons.access_alarm));
          }
          return createchildwidget;
          }
          }





          share|improve this answer
























          • Thanks your answer is correct. Thank you very much.

            – Joe
            Nov 26 '18 at 9:42











          • Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

            – Sameer Carpenter
            Nov 26 '18 at 11:02



















          0














          Try using another Widget while your data is loading :



                  @override
          Widget build(BuildContext context) {
          return documents.isEmpty? Center(child: CircularProgressIndicator()) : GridView.count(
          primary: true,
          padding: EdgeInsets.all(1.0),
          crossAxisCount: 2,
          childAspectRatio: 1.0,
          mainAxisSpacing: 1.0,
          crossAxisSpacing: 1.0,
          children: : createchildwidget(),

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

          );
          }





          share|improve this answer


























          • It shows an error "type Container is not a subtype of type List<widget>"

            – Joe
            Nov 26 '18 at 4:29











          • sorry my bad, I fixed my answer

            – diegoveloper
            Nov 26 '18 at 4:37











          • when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

            – Joe
            Nov 26 '18 at 5:06











          • is there any solutions?

            – Joe
            Nov 26 '18 at 5:38











          • use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

            – anmol.majhail
            Nov 26 '18 at 6:52













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470990%2fflutter-initstate-list-is-not-accessing-another-class-widget%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Instead of making TheGridView as a new class, make it as a function and call it when document data is loaded.
          Try this:



              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 _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: createchildwidget(),

          /* 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> createchildwidget(){
          print("createchildwidget:"+documents.length.toString()); // the value getting 0
          List<Widget> createchildwidget = List<Widget>();
          for(int i=0;i<documents.length;i++){
          createchildwidget.add(makeGridCell(makeGridCell(data[i].data['message'], Icons.access_alarm));
          }
          return createchildwidget;
          }
          }





          share|improve this answer
























          • Thanks your answer is correct. Thank you very much.

            – Joe
            Nov 26 '18 at 9:42











          • Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

            – Sameer Carpenter
            Nov 26 '18 at 11:02
















          0














          Instead of making TheGridView as a new class, make it as a function and call it when document data is loaded.
          Try this:



              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 _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: createchildwidget(),

          /* 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> createchildwidget(){
          print("createchildwidget:"+documents.length.toString()); // the value getting 0
          List<Widget> createchildwidget = List<Widget>();
          for(int i=0;i<documents.length;i++){
          createchildwidget.add(makeGridCell(makeGridCell(data[i].data['message'], Icons.access_alarm));
          }
          return createchildwidget;
          }
          }





          share|improve this answer
























          • Thanks your answer is correct. Thank you very much.

            – Joe
            Nov 26 '18 at 9:42











          • Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

            – Sameer Carpenter
            Nov 26 '18 at 11:02














          0












          0








          0







          Instead of making TheGridView as a new class, make it as a function and call it when document data is loaded.
          Try this:



              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 _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: createchildwidget(),

          /* 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> createchildwidget(){
          print("createchildwidget:"+documents.length.toString()); // the value getting 0
          List<Widget> createchildwidget = List<Widget>();
          for(int i=0;i<documents.length;i++){
          createchildwidget.add(makeGridCell(makeGridCell(data[i].data['message'], Icons.access_alarm));
          }
          return createchildwidget;
          }
          }





          share|improve this answer













          Instead of making TheGridView as a new class, make it as a function and call it when document data is loaded.
          Try this:



              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 _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: createchildwidget(),

          /* 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> createchildwidget(){
          print("createchildwidget:"+documents.length.toString()); // the value getting 0
          List<Widget> createchildwidget = List<Widget>();
          for(int i=0;i<documents.length;i++){
          createchildwidget.add(makeGridCell(makeGridCell(data[i].data['message'], Icons.access_alarm));
          }
          return createchildwidget;
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 9:12









          Sameer CarpenterSameer Carpenter

          389




          389













          • Thanks your answer is correct. Thank you very much.

            – Joe
            Nov 26 '18 at 9:42











          • Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

            – Sameer Carpenter
            Nov 26 '18 at 11:02



















          • Thanks your answer is correct. Thank you very much.

            – Joe
            Nov 26 '18 at 9:42











          • Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

            – Sameer Carpenter
            Nov 26 '18 at 11:02

















          Thanks your answer is correct. Thank you very much.

          – Joe
          Nov 26 '18 at 9:42





          Thanks your answer is correct. Thank you very much.

          – Joe
          Nov 26 '18 at 9:42













          Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

          – Sameer Carpenter
          Nov 26 '18 at 11:02





          Extending state class of a widget is not a good idea. Instead, use functions or create a new widget.

          – Sameer Carpenter
          Nov 26 '18 at 11:02













          0














          Try using another Widget while your data is loading :



                  @override
          Widget build(BuildContext context) {
          return documents.isEmpty? Center(child: CircularProgressIndicator()) : GridView.count(
          primary: true,
          padding: EdgeInsets.all(1.0),
          crossAxisCount: 2,
          childAspectRatio: 1.0,
          mainAxisSpacing: 1.0,
          crossAxisSpacing: 1.0,
          children: : createchildwidget(),

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

          );
          }





          share|improve this answer


























          • It shows an error "type Container is not a subtype of type List<widget>"

            – Joe
            Nov 26 '18 at 4:29











          • sorry my bad, I fixed my answer

            – diegoveloper
            Nov 26 '18 at 4:37











          • when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

            – Joe
            Nov 26 '18 at 5:06











          • is there any solutions?

            – Joe
            Nov 26 '18 at 5:38











          • use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

            – anmol.majhail
            Nov 26 '18 at 6:52


















          0














          Try using another Widget while your data is loading :



                  @override
          Widget build(BuildContext context) {
          return documents.isEmpty? Center(child: CircularProgressIndicator()) : GridView.count(
          primary: true,
          padding: EdgeInsets.all(1.0),
          crossAxisCount: 2,
          childAspectRatio: 1.0,
          mainAxisSpacing: 1.0,
          crossAxisSpacing: 1.0,
          children: : createchildwidget(),

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

          );
          }





          share|improve this answer


























          • It shows an error "type Container is not a subtype of type List<widget>"

            – Joe
            Nov 26 '18 at 4:29











          • sorry my bad, I fixed my answer

            – diegoveloper
            Nov 26 '18 at 4:37











          • when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

            – Joe
            Nov 26 '18 at 5:06











          • is there any solutions?

            – Joe
            Nov 26 '18 at 5:38











          • use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

            – anmol.majhail
            Nov 26 '18 at 6:52
















          0












          0








          0







          Try using another Widget while your data is loading :



                  @override
          Widget build(BuildContext context) {
          return documents.isEmpty? Center(child: CircularProgressIndicator()) : GridView.count(
          primary: true,
          padding: EdgeInsets.all(1.0),
          crossAxisCount: 2,
          childAspectRatio: 1.0,
          mainAxisSpacing: 1.0,
          crossAxisSpacing: 1.0,
          children: : createchildwidget(),

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

          );
          }





          share|improve this answer















          Try using another Widget while your data is loading :



                  @override
          Widget build(BuildContext context) {
          return documents.isEmpty? Center(child: CircularProgressIndicator()) : GridView.count(
          primary: true,
          padding: EdgeInsets.all(1.0),
          crossAxisCount: 2,
          childAspectRatio: 1.0,
          mainAxisSpacing: 1.0,
          crossAxisSpacing: 1.0,
          children: : createchildwidget(),

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

          );
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 4:37

























          answered Nov 25 '18 at 19:52









          diegoveloperdiegoveloper

          13.4k12032




          13.4k12032













          • It shows an error "type Container is not a subtype of type List<widget>"

            – Joe
            Nov 26 '18 at 4:29











          • sorry my bad, I fixed my answer

            – diegoveloper
            Nov 26 '18 at 4:37











          • when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

            – Joe
            Nov 26 '18 at 5:06











          • is there any solutions?

            – Joe
            Nov 26 '18 at 5:38











          • use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

            – anmol.majhail
            Nov 26 '18 at 6:52





















          • It shows an error "type Container is not a subtype of type List<widget>"

            – Joe
            Nov 26 '18 at 4:29











          • sorry my bad, I fixed my answer

            – diegoveloper
            Nov 26 '18 at 4:37











          • when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

            – Joe
            Nov 26 '18 at 5:06











          • is there any solutions?

            – Joe
            Nov 26 '18 at 5:38











          • use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

            – anmol.majhail
            Nov 26 '18 at 6:52



















          It shows an error "type Container is not a subtype of type List<widget>"

          – Joe
          Nov 26 '18 at 4:29





          It shows an error "type Container is not a subtype of type List<widget>"

          – Joe
          Nov 26 '18 at 4:29













          sorry my bad, I fixed my answer

          – diegoveloper
          Nov 26 '18 at 4:37





          sorry my bad, I fixed my answer

          – diegoveloper
          Nov 26 '18 at 4:37













          when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

          – Joe
          Nov 26 '18 at 5:06





          when adding the above code. It is not entering in "createchildwidget()" and progress bar show without stopping.

          – Joe
          Nov 26 '18 at 5:06













          is there any solutions?

          – Joe
          Nov 26 '18 at 5:38





          is there any solutions?

          – Joe
          Nov 26 '18 at 5:38













          use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

          – anmol.majhail
          Nov 26 '18 at 6:52







          use FutureBuilder, it way much clean & better way here. docs.flutter.io/flutter/widgets/FutureBuilder-class.html Firestore.instance.collection("messages").getDocuments() - return Future

          – anmol.majhail
          Nov 26 '18 at 6:52




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470990%2fflutter-initstate-list-is-not-accessing-another-class-widget%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin