如何修复“必须向文本小部件提供非空字符串”。颤振错误?

Posted

技术标签:

【中文标题】如何修复“必须向文本小部件提供非空字符串”。颤振错误?【英文标题】:how to fix "A non-null String must be provided to a Text widget." error in flutter? 【发布时间】:2020-12-07 04:18:37 【问题描述】:

我正在实现颤振网站 [link]https://api.flutter.dev/flutter/widgets/ValueListenableBuilder-class.html 中给出的基本代码我收到错误:“必须向文本小部件提供非空字符串”。文本小部件被赋予增量计数器的值。那么,您能否解释一下为什么会出现 null 问题?

代码:

import 'package:flutter/material.dart';


void main() 
  runApp(MyHomePage());


class MyHomePage extends StatefulWidget 
  MyHomePage(Key key, this.title) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  final ValueNotifier<int> _counter = ValueNotifier<int>(0);
  final Widget goodJob = const Text('Good job!');
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title)
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            ValueListenableBuilder(
              builder: (BuildContext context, int value, Widget child) 
                // This builder will only get called when the _counter
                // is updated.
                return Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Text('$value'),
                    child,
                  ],
                );
              ,
              valueListenable: _counter,
              // The child parameter is most helpful if the child is
              // expensive to build and does not depend on the value from
              // the notifier.
              child: goodJob,
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.plus_one),
        onPressed: () => _counter.value += 1,
      ),
    );
  

【问题讨论】:

【参考方案1】:

您收到错误是因为您使用 title 字符串作为 AppBar's 标题并且您没有给它任何值:

我添加了一个以你的代码为例的演示:

void main() 
  runApp(MyApp());


class MyApp extends StatelessWidget 
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(
        title: 'My Home Page',
      ),
    );
  


class MyHomePage extends StatefulWidget 
  MyHomePage(Key key, this.title) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  final ValueNotifier<int> _counter = ValueNotifier<int>(0);
  final Widget goodJob = const Text('Good job!');
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            ValueListenableBuilder(
              builder: (BuildContext context, int value, Widget child) 
                // This builder will only get called when the _counter
                // is updated.
                return Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Text('$value'),
                    child,
                  ],
                );
              ,
              valueListenable: _counter,
              // The child parameter is most helpful if the child is
              // expensive to build and does not depend on the value from
              // the notifier.
              child: goodJob,
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.plus_one),
        onPressed: () => _counter.value += 1,
      ),
    );
  

【讨论】:

得到不同的错误:在构建 MyHomePage(state: _MyHomePageState#532de): MediaQuery.of() 时引发了以下断言,使用不包含 MediaQuery 的上下文调用。 我用您的示例的完整演示更新了我的答案,您的小部件树中应该有一个 MaterialApp

以上是关于如何修复“必须向文本小部件提供非空字符串”。颤振错误?的主要内容,如果未能解决你的问题,请参考以下文章

必须向 Text 小部件提供非空字符串

如何修复颤振医生不识别安卓手机

如何修复颤振 lib/main.dart 错误

如何修复颤振中的“回复已提交的错误”?

如何在颤振中修复“Android许可证状态未知”

如何修复颤振的 AndroidX 不兼容问题?