简体   繁体   English

在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'data'。 接收方:null 尝试调用:数据

[英]The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was called on null. Receiver: null Tried calling: data

I'm trying to connect the article page to the search page body content, hope to be clear, but I get the error我正在尝试将文章页面连接到搜索页面正文内容,希望清楚,但我收到错误

The following NoSuchMethodError was thrown building Builder(dirty): The getter 'data' was called on null.在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'data'。 Receiver: null Tried calling: data接收方:null 尝试调用:数据

Anyone can solve it?任何人都可以解决它? Thanks in advance提前致谢

class _SearchPageState extends State<SearchPage> {
  List<Article> _searchedPost = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: TextField(
          style: const TextStyle(color: Colors.white),
          decoration: const InputDecoration(
              hintText: 'Search Post',
              hintStyle: TextStyle(color: Colors.white),
              border: InputBorder.none),
          onChanged: (val) {
            setState(() {
              _searchedPost =
                  widget.posts.where((el) => el.title.contains(val)).toList();
            });
          },
        ),
      ),

      /// Body
      body: _searchedPost.isEmpty
          ? Center(
              child: Text(
                'Nessun articolo disponibile',
                textAlign: TextAlign.center,
                style: Theme.of(context).textTheme.headline6,
              ),
            )
          : ListView.builder(
              itemCount: _searchedPost.length,
              itemBuilder: (context, i) {
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Card(
                      margin: const EdgeInsets.all(10),
                      elevation: 5,
                      shadowColor: Colors.black26,
                      color: Colors.white,
                      child: InkWell(
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(10),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              _searchedPost[i].urlImage == null
                                  ? const Text("Nessuna immagine caricata")
                                  : Image.network(
                                      _searchedPost[i].urlImage,
                                      width: double.infinity,
                                      height: 220,
                                      fit: BoxFit.cover,
                                    ),
                              // Title article
                              Column(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        left: 16, top: 16, bottom: 16),
                                    child: Row(
                                      children: [
                                        Expanded(
                                          child: Text(
                                            _searchedPost[i].title,
                                            maxLines: 3,
                                            overflow: TextOverflow.clip,
                                            softWrap: true,
                                            style: const TextStyle(
                                              fontSize: 18,
                                              fontWeight: FontWeight.w600,
                                              fontFamily: "Raleway",
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => ArticlePage(
                                data: snapshot.data?[i],
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                  ],
                );
              },
            ),
    );
  }
}

Your data has never assigned.您的数据从未分配过。 It is better to make it nullable and check while using it.最好将其设为可空并在使用时进行检查。

YourDataType? data;

There is no snapshoot variable I can see.我看不到snapshoot变量。 You might want to pass _searchedPost[i]你可能想通过_searchedPost[i]

Try like this on your on Tap, replace it on OnTap在你的 on Tap 上尝试这样,在OnTap上替换它

onTap: () {
  if (_searchedPost[i] != null) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ArticlePage(
          data: _searchedPost[i],
        ),
      ),
    );
  }
},

And on ArticlePageArticlePage

class ArticlePage extends StatelessWidget {
  final Article data; 
  const ArticlePage({
   Key? key,
   required this.data,
  })

And

Image.network( data.urlImage,
Text(data.title

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在构建 Builder(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;=”。 接收器:null 相关的错误原因是: - The following NoSuchMethodError was thrown building Builder(dirty): The method '>=' was called on null. Receiver: null The relevant error-causing was: 在构建 Home() 时引发了以下 NoSuchMethodError:在 null 上调用了方法“&gt;”。 接收方:null 尝试呼叫:&gt;(1) - The following NoSuchMethodError was thrown building Home(): The method '>' was called on null. Receiver: null Tried calling: >(1) 在 null 上调用了 getter 'data'。 接收方:null 尝试调用:数据 - The getter 'data' was called on null. Receiver: null Tried calling: data 我该如何解决这个问题? 构建时引发了以下 NoSuchMethodError:在 null 上调用了方法“[]”。 接收方:null 尝试调用:[](0) - How do i fix this issue? The following NoSuchMethodError was thrown building: The method '[]' was called on null. Receiver: null Tried calling: [](0) 在构建 MessageBubble(dirty) 时引发了以下 NoSuchMethodError:在 null 上调用了 getter 'millisecondsSinceEpoch' - The following NoSuchMethodError was thrown building MessageBubble(dirty): The getter 'millisecondsSinceEpoch' was called on null 在构建 Builder 时抛出了以下 NoSuchMethodError:getter &#39;email&#39; 在 null 上被调用。 在火力基地 - The following NoSuchMethodError was thrown building Builder: The getter 'email' was called on null. in firebase NoSuchMethodError:在 null 上调用了 getter 'title'。 接收器:null。 尝试调用:标题。 //有人请 - NoSuchMethodError : The getter 'title' was called on null. Receiver : null. Tried calling: title. //Anyone please NoSuchMethodError(NoSuchMethodError:在 null 上调用了方法“[]”。接收方:null 尝试调用:[](0)) - NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: [](0)) Flutter:未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'id'。 接收方:null 尝试呼叫:id - Flutter: Unhandled Exception: NoSuchMethodError: The getter 'id' was called on null. Receiver: null Tried calling: id 在 null 上调用了方法“数据”。 接收方:null 尝试调用:data() - The method 'data' was called on null. Receiver: null Tried calling: data()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM