我有一个应用程序,想要查看广告创建者的用户名,但是运行它时遇到了一些问题,我收到此错误:
A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 298 pos 10: 'data != null'
The relevant error-causing widget was: AdView file:///Users/ahmedhussain/Downloads/khamsat/Client%20Apps/HPX-KSA/hpx_ksa/lib/Screens/custom_ad_tile.dart:23:79 When the exception was thrown, this was the stack:
#2 new Text (package:flutter/src/widgets/text.dart:298:10)
#3 _AdViewState.build (package:hpxksa/Screens/ad_view.dart:134:34)
尽管我检查了数据库,并且文档中已经有数据,但是如果我将值更改为文档中的任何其他值,它将正常工作。
这是我的代码:
class AdView extends StatefulWidget {
final AdModel adModel;
final String submittedStr;
AdView({this.adModel, this.submittedStr});
@override
_AdViewState createState() => _AdViewState();
}
return StreamProvider<List<AdModel>>.value(
value: DatabaseService().ads,
child: Scaffold(
body: Column(
children: <Widget>[
Row(
children: <Widget>[
InkWell(
child: Text(widget.adModel.username , style: TextStyle(color: kPrimaryColor),),
onTap: (){},
),
InkWell(
child: Text(widget.adModel.location, style: TextStyle(color: kPrimaryColor),),
onTap: (){},
),
这里的AdModel类:
class AdModel{
String category;
String location;
String adName;
String adImage;
String userId;
String username;
AdModel({
this.category,
this.location,
this.adName,
this.adImage,
this.userId,
this.username,
});
这是我如何通过AdView传递AdModel:
class CustomAdTile extends StatefulWidget {
final AdModel adModel;
CustomAdTile({this.adModel});
@override
_CustomAdTileState createState() => _CustomAdTileState();
}
class _CustomAdTileState extends State<CustomAdTile> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 5.0),
child: InkWell(
onTap: (){
print('card pressed');
Navigator.push(context, new MaterialPageRoute(builder: (context) => AdView(adModel: widget.adModel,)));
},
在此处创建AdModel并在提供者中使用的AdList:
class _AdsListState extends State<AdsList> {
@override
Widget build(BuildContext context) {
final ads = Provider.of<List<AdModel>>(context);
return ListView.builder(
scrollDirection: Axis.vertical,
itemCount:(ads == null) ? 0 : ads.length,
itemBuilder: (context, index){
return
CustomAdTile(adModel: ads[index],);
});
}
}
答案 0 :(得分:0)
您可以打印widget.adModel.username
来查看它是否真的null
,并且还应该像这样null check
进行widget?.adModel?.username ?? 'value is null'
return StreamProvider<List<AdModel>>.value(
value: DatabaseService().ads,
child: Scaffold(
body: Column(
children: <Widget>[
Row(
children: <Widget>[
InkWell(
child: Text(widget?.adModel?.username ?? 'username is null' , style: TextStyle(color: kPrimaryColor),),
onTap: (){},
),
InkWell(
child: Text(widget?.adModel?.location ?? 'location is null', style: TextStyle(color: kPrimaryColor),),
onTap: (){},
),
答案 1 :(得分:0)
widget.adModel没有为您提供更新的值,您获取的变量的默认值为null
Text(widget.adModel.username?? "" , style: TextStyle(color: kPrimaryColor),),
Text(widget.adModel.location?? "", style: TextStyle(color: kPrimaryColor),),
答案 2 :(得分:0)
这是因为因为widget.adModel.username为null。您可以通过执行此操作来解决该错误
Text('${widget.adModel.username}' ?? 'Username not available' , style: TextStyle(color: kPrimaryColor),
Text('${widget.adModel.location}' ?? 'Location not available', style: TextStyle(color: kPrimaryColor),
??这基本上意味着如果第一个为null,则使用第二个字符串