简体   繁体   中英

How to get rid of the exception initial value was called on null

Hi i am trying to fetch the images from firebase then returning it in a Carousel slider.builder but whenever app loads it throws the following exception. The images are stored in Firebase. I could not figure out why its not fetching the detials. The attributes are correct but still its not fetching the image from firebase.

I/flutter (13636): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (13636): The following NoSuchMethodError was thrown building FutureBuilder<dynamic>(state:
I/flutter (13636): _FutureBuilderState<dynamic>#eb8ff):
I/flutter (13636): The getter 'initialPage' was called on null.
I/flutter (13636): Receiver: null
I/flutter (13636): Tried calling: initialPage
I/flutter (13636): 
I/flutter (13636): The relevant error-causing widget was:
I/flutter (13636):   FutureBuilder<dynamic>
I/flutter (13636):   file:///C:/Users/pranjal.kumar.deb/AndroidStudioProjects/home_page/lib/main.dart:79:19
I/flutter (13636): "

This is my code


import 'package:flutter/material.dart';
import 'package:carousel_pro/carousel_pro.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:home_page/Allitem_Screen.dart';
import 'package:carousel_slider/carousel_controller.dart';
import 'package:carousel_slider/carousel_options.dart';
import 'package:carousel_slider/carousel_slider.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {





  @override
  Widget build(BuildContext context) {

    ////Image Carousel Widget////
   

    Future getImageFromFirebase() async {
      var firestore = Firestore.instance;
      QuerySnapshot qn = await firestore.collection("AllimageSlider").getDocuments();
      return qn.documents;

    }

   
    ////Widget Carousel/////
    return MaterialApp(
      debugShowCheckedModeBanner: false,

      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(backgroundColor: Colors.red.withGreen(10),
            title: Padding(
              padding: const EdgeInsets.all(1),
              child: Text('SDE',style: TextStyle(color: Colors.white),

              ),

            ),
            titleSpacing: 50.0,
          ),
          body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Container(
              height: 1350,
              color: Colors.white,
              child: Column(
                children: <Widget>[

                  FutureBuilder(
                    future: getImageFromFirebase(),
                    builder: (context,snapshot){
                      if (snapshot.hasData){
                        return CarouselSlider.builder(
                        itemCount: snapshot.data.length,
                        itemBuilder: (context,index){
                          DocumentSnapshot doc = snapshot.data[index];
                          return Container(
                            child: Image.network(doc['imageUrl']),
                          );
                        },

                        );
                      }
                      return Text('Loading');
                    },
                  ),

                   //// image carousel Widget
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0,20,0,0),
                    child: Container(child: Text('Sugar, Jaggery & Salt',style: TextStyle(fontSize: 20.0,fontWeight: FontWeight.bold),), alignment: Alignment.bottomLeft,

                    ),
                  ),
                  Container(
                    height: 150,

                      child: StreamBuilder(
                          stream: Firestore.instance.collection('SugarJaggerySalt').snapshots(),
                          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                                if (snapshot.hasData){
                                    return ListView.builder(
                                      scrollDirection: Axis.horizontal,
                                      itemCount: snapshot.data.documents.length,
                                        itemBuilder: (context, index){
                                          DocumentSnapshot rawItems = snapshot.data.documents[index];
                                          return Container(
                                            width: 160.0,
                                            child: InkWell(

                                              onTap: (){
                                                print('Kak Babu'+index.toString());
                                                print(rawItems["UID"]);
                                                Navigator.push(context, MaterialPageRoute(builder: (context)=>AllItemScreen(itemName:rawItems["UID"],)));

                                              },
                                              child: Card(
                                                child: Wrap(
                                                  children: <Widget>[
                                                    Container(
                                                      margin: EdgeInsets.all(10.0),
                                                      child: Image.network(rawItems["image"],
                                                        height: 80,
                                                        width: 160,
                                                      ),
                                                    ),
                                                    ListTile(
                                                      title: Row(
                                                        mainAxisAlignment: MainAxisAlignment
                                                            .spaceEvenly,
                                                        children: <Widget>[

                                                          Text(rawItems["UID"],
                                                            textAlign: TextAlign.center,),

                                                        ],
                                                      ),

                                                    )
                                                  ],
                                                ),
                                              ),
                                            ),
                                          );


                                        }



                                    );
                                }

                                else{
                                  return  Center(
                                    child: Container(),
                                  );
                                }
                          }
                      ),

                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0,20,0,0),
                    child: Container(child: Text('Sugar, Jaggery & Salt',style: TextStyle(fontSize: 20.0,fontWeight: FontWeight.bold),), alignment: Alignment.bottomLeft,

                    ),
                  ),
                  Container(
                    height: 150,

                      child: StreamBuilder(
                          stream: Firestore.instance.collection('SugarJaggerySalt').snapshots(),
                          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                            if (snapshot.hasData){
                              return ListView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: snapshot.data.documents.length,
                                  itemBuilder: (context, index){
                                    DocumentSnapshot rawItems = snapshot.data.documents[index];
                                    return Container(
                                      width: 160.0,
                                      child: InkWell(

                                        onTap: (){
                                          print('Kak Babu'+index.toString());
                                          print(rawItems["UID"]);
                                          Navigator.push(context, MaterialPageRoute(builder: (context)=>AllItemScreen(itemName:rawItems["UID"],)));

                                        },
                                        child: Card(
                                          child: Wrap(
                                            children: <Widget>[
                                              Container(
                                                margin: EdgeInsets.all(10.0),
                                                child: Image.network(rawItems["image"],
                                                  height: 80,
                                                  width: 160,
                                                ),
                                              ),
                                              ListTile(
                                                title: Row(
                                                  mainAxisAlignment: MainAxisAlignment
                                                      .spaceEvenly,
                                                  children: <Widget>[

                                                    Text(rawItems["UID"],
                                                      textAlign: TextAlign.center,),

                                                  ],
                                                ),

                                              )
                                            ],
                                          ),
                                        ),
                                      ),
                                    );


                                  }



                              );
                            }

                            else{
                              return  Center(
                                child: Container(),
                              );
                            }
                          }
                      ),
                    ),


                  ////Category 1 widget

                  //// Category 2 Widget
                                          //// Category 3



                ],
              ),
            ),
          ),


        ),
      ),
    );
  }
}


在此处输入图像描述

Hi i am trying to fetch the images from firebase then returning it in a Carousel slider.builder but whenever app loads it throws the following exception. The images are stored in Firebase. I could not figure out why its not fetching the detials. The attributes are correct but still its not fetching the image from firebase.

I/flutter (13636): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (13636): The following NoSuchMethodError was thrown building FutureBuilder<dynamic>(state:
I/flutter (13636): _FutureBuilderState<dynamic>#eb8ff):
I/flutter (13636): The getter 'initialPage' was called on null.
I/flutter (13636): Receiver: null
I/flutter (13636): Tried calling: initialPage
I/flutter (13636): 
I/flutter (13636): The relevant error-causing widget was:
I/flutter (13636):   FutureBuilder<dynamic>
I/flutter (13636):   file:///C:/Users/pranjal.kumar.deb/AndroidStudioProjects/home_page/lib/main.dart:79:19
I/flutter (13636): "

This is my code


import 'package:flutter/material.dart';
import 'package:carousel_pro/carousel_pro.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:home_page/Allitem_Screen.dart';
import 'package:carousel_slider/carousel_controller.dart';
import 'package:carousel_slider/carousel_options.dart';
import 'package:carousel_slider/carousel_slider.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {





  @override
  Widget build(BuildContext context) {

    ////Image Carousel Widget////
   

    Future getImageFromFirebase() async {
      var firestore = Firestore.instance;
      QuerySnapshot qn = await firestore.collection("AllimageSlider").getDocuments();
      return qn.documents;

    }

   
    ////Widget Carousel/////
    return MaterialApp(
      debugShowCheckedModeBanner: false,

      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(backgroundColor: Colors.red.withGreen(10),
            title: Padding(
              padding: const EdgeInsets.all(1),
              child: Text('SDE',style: TextStyle(color: Colors.white),

              ),

            ),
            titleSpacing: 50.0,
          ),
          body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Container(
              height: 1350,
              color: Colors.white,
              child: Column(
                children: <Widget>[

                  FutureBuilder(
                    future: getImageFromFirebase(),
                    builder: (context,snapshot){
                      if (snapshot.hasData){
                        return CarouselSlider.builder(
                        itemCount: snapshot.data.length,
                        itemBuilder: (context,index){
                          DocumentSnapshot doc = snapshot.data[index];
                          return Container(
                            child: Image.network(doc['imageUrl']),
                          );
                        },

                        );
                      }
                      return Text('Loading');
                    },
                  ),

                   //// image carousel Widget
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0,20,0,0),
                    child: Container(child: Text('Sugar, Jaggery & Salt',style: TextStyle(fontSize: 20.0,fontWeight: FontWeight.bold),), alignment: Alignment.bottomLeft,

                    ),
                  ),
                  Container(
                    height: 150,

                      child: StreamBuilder(
                          stream: Firestore.instance.collection('SugarJaggerySalt').snapshots(),
                          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                                if (snapshot.hasData){
                                    return ListView.builder(
                                      scrollDirection: Axis.horizontal,
                                      itemCount: snapshot.data.documents.length,
                                        itemBuilder: (context, index){
                                          DocumentSnapshot rawItems = snapshot.data.documents[index];
                                          return Container(
                                            width: 160.0,
                                            child: InkWell(

                                              onTap: (){
                                                print('Kak Babu'+index.toString());
                                                print(rawItems["UID"]);
                                                Navigator.push(context, MaterialPageRoute(builder: (context)=>AllItemScreen(itemName:rawItems["UID"],)));

                                              },
                                              child: Card(
                                                child: Wrap(
                                                  children: <Widget>[
                                                    Container(
                                                      margin: EdgeInsets.all(10.0),
                                                      child: Image.network(rawItems["image"],
                                                        height: 80,
                                                        width: 160,
                                                      ),
                                                    ),
                                                    ListTile(
                                                      title: Row(
                                                        mainAxisAlignment: MainAxisAlignment
                                                            .spaceEvenly,
                                                        children: <Widget>[

                                                          Text(rawItems["UID"],
                                                            textAlign: TextAlign.center,),

                                                        ],
                                                      ),

                                                    )
                                                  ],
                                                ),
                                              ),
                                            ),
                                          );


                                        }



                                    );
                                }

                                else{
                                  return  Center(
                                    child: Container(),
                                  );
                                }
                          }
                      ),

                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0,20,0,0),
                    child: Container(child: Text('Sugar, Jaggery & Salt',style: TextStyle(fontSize: 20.0,fontWeight: FontWeight.bold),), alignment: Alignment.bottomLeft,

                    ),
                  ),
                  Container(
                    height: 150,

                      child: StreamBuilder(
                          stream: Firestore.instance.collection('SugarJaggerySalt').snapshots(),
                          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                            if (snapshot.hasData){
                              return ListView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: snapshot.data.documents.length,
                                  itemBuilder: (context, index){
                                    DocumentSnapshot rawItems = snapshot.data.documents[index];
                                    return Container(
                                      width: 160.0,
                                      child: InkWell(

                                        onTap: (){
                                          print('Kak Babu'+index.toString());
                                          print(rawItems["UID"]);
                                          Navigator.push(context, MaterialPageRoute(builder: (context)=>AllItemScreen(itemName:rawItems["UID"],)));

                                        },
                                        child: Card(
                                          child: Wrap(
                                            children: <Widget>[
                                              Container(
                                                margin: EdgeInsets.all(10.0),
                                                child: Image.network(rawItems["image"],
                                                  height: 80,
                                                  width: 160,
                                                ),
                                              ),
                                              ListTile(
                                                title: Row(
                                                  mainAxisAlignment: MainAxisAlignment
                                                      .spaceEvenly,
                                                  children: <Widget>[

                                                    Text(rawItems["UID"],
                                                      textAlign: TextAlign.center,),

                                                  ],
                                                ),

                                              )
                                            ],
                                          ),
                                        ),
                                      ),
                                    );


                                  }



                              );
                            }

                            else{
                              return  Center(
                                child: Container(),
                              );
                            }
                          }
                      ),
                    ),


                  ////Category 1 widget

                  //// Category 2 Widget
                                          //// Category 3



                ],
              ),
            ),
          ),


        ),
      ),
    );
  }
}


在此处输入图像描述

I figured out there is property called option in the image carousel slider it was null that was the reason for the error

options: CarouselOptions( height: 400, aspectRatio: 16/9, viewportFraction: 0.8, initialPage: 0, enableInfiniteScroll: true, reverse: false, autoPlay: true, autoPlayInterval: Duration(seconds: 3), autoPlayAnimationDuration: Duration(milliseconds: 800), autoPlayCurve: Curves.fastOutSlowIn, enlargeCenterPage: true,

                          scrollDirection: Axis.horizontal,
                        ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM