Flutter:无法从Firebase读取数据.在空错误上调用了方法"[]" [英] Flutter: Unable to read data from firebase. The method '[]' was called on null error
问题描述
我正在尝试在扫描条形码后从Firebase读取数据.
这应该是它的显示方式,但是应该显示数据库中的名称和价格,而不是条形码(
代码:
class ListTileModel {字符串条形码;ListTileModel(this.barcode);}
下面的代码在有状态的小部件内
List< ListTileModel>_items = [];字符串条形码=";无效_add(){_items.add(ListTileModel(barcode));setState((){});}@override无效的initState(){super.initState();}
使用的StreamBuiler:
新容器(子级:ListView(孩子:_items.map((item)=> StreamBuilder(流:FirebaseDatabase.instance.reference().child('prd').child(item.barcode).onValue,生成器:(上下文,快照){打印(item.barcode);if(snap.hasData){DataSnapshot快照= snap.data.snapshot;映射<动态,动态>itm = snapshot.value;返回snap.data.snapshot.value == null?SizedBox():ListView.builder(rinkWrap:是的,scrollDirection:Axis.vertical,itemCount:1itemBuilder :(上下文,索引){返回行(子代:< Widget> [ConstrainedBox(子代:集装箱(子代:Text(itm [index] ['name'],样式:TextStyle(fontSize:20),),),),SizedBox(width:100,),ConstrainedBox(子代:集装箱(子代:Text(itm [index] ['price'].toString(),样式:TextStyle(fontSize:20),),),),],);},);}别的 {返回中心(子级:CircularProgressIndicator());}}),).toList()),),
条形码扫描代码:
小工具:
floatingActionButton:FloatingActionButton(onPressed:扫描,子级:Icon(Icons.add_shopping_cart),),
scan():
未来scan()异步{尝试{字符串条形码=等待BarcodeScanner.scan();setState((){this.barcode =条码;});_添加();}在PlatformException catch(e){if(e.code == BarcodeScanner.CameraAccessDenied){setState((){this.barcode ='拒绝访问';});}}
我正在关注错误:
以下NoSuchMethodError被抛出:方法'[]'在null上被调用.接收者:null尝试致电:
请尝试一下,让我知道它是否解决了您需要更改的问题
Text(itm [index] ['name'],样式:TextStyle(fontSize:20),),Text(itm [index] ['price'].toString(),样式:TextStyle(fontSize:20),),
收件人
Text(itm ['det'] ['name'],样式:TextStyle(fontSize:20),),Text(itm ['det'] ['price'].toString(),样式:TextStyle(fontSize:20),),
让我知道这是否对您有用.我相信问题也出在索引上.现在你的话.
I am trying to read the data from firebase after scanning a barcode.
This is how it should appear, but instead of barcode, it should display name and price from the database (https://m.imgur.com/gallery/lEFJZ0Q)
Code:
class ListTileModel {
String barcode;
ListTileModel(this.barcode);
}
the below code is inside the stateful widget
List<ListTileModel> _items = [];
String barcode = "";
void _add() {
_items.add(ListTileModel(barcode));
setState(() {});
}
@override
void initState(){
super.initState();
}
StreamBuiler Used:
new Container(
child: ListView(
children: _items
.map((item) => StreamBuilder(
stream: FirebaseDatabase.instance.reference().child('prd').child(item.barcode).onValue,
builder: (context, snap) {
print(item.barcode);
if(snap.hasData){
DataSnapshot snapshot = snap.data.snapshot;
Map<dynamic, dynamic> itm = snapshot.value;
return snap.data.snapshot.value == null
? SizedBox()
: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: 1,
itemBuilder: (context, index) {
return Row(
children: <Widget>[
ConstrainedBox(
child: Container(
child: Text(itm[index]['name'],style: TextStyle(fontSize: 20),),
),
),
SizedBox(width: 100,),
ConstrainedBox(
child: Container(
child: Text(itm[index]['price'].toString(),style: TextStyle(fontSize: 20),),
),
),
],
);
},
);
}
else {
return Center(child: CircularProgressIndicator());
}
}
),
).toList()
),
),
The Barcode Scan code:
widget:
floatingActionButton: FloatingActionButton(
onPressed: scan,
child: Icon(Icons.add_shopping_cart),
),
scan() :
Future scan() async{
try{
String barcode = await BarcodeScanner.scan();
setState(() {
this.barcode = barcode;
});
_add();
}on PlatformException catch(e) {
if(e.code == BarcodeScanner.CameraAccessDenied){
setState(() {
this.barcode = 'Access Denied';
});
}
}
I'm am getting following Error:
The following NoSuchMethodError was thrown building: The method '[]' was called on null. Receiver: null Tried calling:
Please try this and let me know if it fixes your problem you need to change
Text(itm[index]['name'],style: TextStyle(fontSize: 20),),
Text(itm[index]['price'].toString(),style: TextStyle(fontSize: 20),),
To
Text(itm['det']['name'],style: TextStyle(fontSize: 20),),
Text(itm['det']['price'].toString(),style: TextStyle(fontSize: 20),),
Let me know if this works for you. I believe the problem is the index also. right now your saying.
这篇关于Flutter:无法从Firebase读取数据.在空错误上调用了方法"[]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!