firebase - 如何摆脱异常初始值被称为null
问题描述
嗨,我正在尝试从 firebase 获取图像,然后在 Carousel slider.builder 中返回它,但是每当应用程序加载时,它都会引发以下异常。图像存储在 Firebase 中。我无法弄清楚为什么它不获取详细信息。属性是正确的,但仍然没有从 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): "
这是我的代码
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
],
),
),
),
),
),
);
}
}
解决方案
我发现图像轮播滑块中有一个名为 option 的属性,它是空的,这是错误的原因
选项:CarouselOptions(高度:400,aspectRatio:16/9,viewportFraction:0.8,initialPage:0,enableInfiniteScroll:true,reverse:false,autoPlay:true,autoPlayInterval:持续时间(秒:3),autoPlayAnimationDuration:持续时间(毫秒:800 ),autoPlayCurve:Curves.fastOutSlowIn,放大中心页面:真,
scrollDirection: Axis.horizontal,
),
推荐阅读
- javascript - 如何在 reactjs 中使用 javascript 更改样式
- html - onload函数和onclick在jsp中不起作用
- javascript - Javascript 文件到 Blob
- python - 在 requests.post() python3 上接收错误
- angular - css 后加载角度数据,破坏设计
- python - return func(*args, **kwargs) ValueError: read of closed file
- jquery - MVC - ajax 调用中的历史 API
- powershell - 使用 PowerShell 进行 32 位磁盘碎片整理和病毒扫描命令
- mysql - MySQL 子查询返回超过 1 行错误
- c# - 在安装时为没有管理员密码的 Windows 窗体应用程序创建设置