【问题标题】:Flutter: A non-null String must be provided to a Text widgetFlutter:必须向 Text 小部件提供非空字符串
【发布时间】:2020-06-29 13:17:02
【问题描述】:

我有一个应用程序,想查看广告创建者的用户名,但是我在运行它时遇到了一点问题,我收到了这个错误:

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,)));
    },

这里是在 Provider 中创建和使用 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],);
        });
  }
}

【问题讨论】:

  • 你可以使用类似的东西:Text(widget?.adModel?.username ?? "Undefined")

标签: flutter dart widget


【解决方案1】:

你可以打印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: (){},
                        ),

【讨论】:

  • 您打印了widget.adModel.username,它显示了一个值,或者您打印了widget.adModel。你打印了哪些?
  • 我打印了 widget.adModel.username 它显示了价值
  • 这意味着widget.adModel.username 为空。你的模型有问题
【解决方案2】:

widget.adModel 没有为您提供更新的值,您将变量的默认值设为 null

Text(widget.adModel.username?? ""  , style: TextStyle(color: kPrimaryColor),),

Text(widget.adModel.location?? "", style: TextStyle(color: kPrimaryColor),),

【讨论】:

    【解决方案3】:

    这是因为 widget.adModel.username 为空。你可以通过这样做来解决这个错误

    Text('${widget.adModel.username}' ?? 'Username not available' , style: TextStyle(color: kPrimaryColor),
    
    Text('${widget.adModel.location}' ?? 'Location not available', style: TextStyle(color: kPrimaryColor),
    

    ??这基本上意味着如果第一个为空,则使用第二个字符串

    【讨论】:

    • 能否分享一下 Admodel 类结构,以及传递这些参数的代码
    • 我编辑了代码尝试使用它,看看它是否有效
    • 我添加了 AdModel 以及我如何传递它,我尝试了你修改过的代码,但也没有用
    • 你能告诉我你创建admodel对象并将值传递给构造函数的代码吗
    • 我添加了如何使用 adModel 作为提供者的列表
    猜你喜欢
    • 2020-04-14
    • 2021-01-28
    • 2020-11-18
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2021-03-03
    相关资源
    最近更新 更多