【问题标题】: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,
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案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
    猜你喜欢
    • 2021-02-14
    • 2021-03-03
    • 2021-03-17
    • 2020-08-10
    • 2021-10-11
    • 2021-08-29
    • 2021-03-07
    • 2021-05-11
    • 2022-11-17
    相关资源
    最近更新 更多