简体   繁体   English

为什么我的小部件 null 上的参数传递?

[英]Why is the parameter pass on my widget null?

I am new in Flutter.我是 Flutter 的新手。 So, I created a class widget code below.所以,我在下面创建了一个 class 小部件代码。

import 'package:flutter/material.dart';

class BoxBuilder extends StatefulWidget {
  BoxBuilder({Key key, @required this.text}): super(key: key);
  String text;

  @override
  _BoxBuilderState createState() => _BoxBuilderState();
}

class _BoxBuilderState extends State<BoxBuilder>  {
  _BoxBuilderState({this.text});

  String text;

  double getSize() {
    if (MediaQuery.of(context).orientation == Orientation.portrait) {
      return MediaQuery.of(context).size.width - 120;
    } else {
      return MediaQuery.of(context).size.height - 120;
    }
  }

  @override
  Widget build(BuildContext context) {
    double _width = getSize() / 2;
    double _height = getSize() / 3;
    return Container(
      width: _width,
      height: _height,
      child: Card(
        color: Colors.white,
        child: Center(
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Text(
              text==null? 'hello': text,   // LINE 1100
            ),
          ),
        ),
      ),
    );
  }
}

I have a list of strings from another class widget declared as我有一个来自另一个 class 小部件的字符串列表,声明为

List<String> myFoos = [
    'one foo',
    'another foo',
    'more foo',
    'some foo',
    'next foo',
    'all foo'
  ];

passed each list as parameter to my class BoxBuilder widget将每个列表作为参数传递给我的 class BoxBuilder 小部件

             Center(
                child: Container(
                  width: getSize(),
                  height: getSize(),
                  child: Wrap(
                    children: [
                      BoxBuilder(text:myFoos[0]),  //LINE 1001
                      BoxBuilder(text:myFoos[1]),  //LINE 1002
                      BoxBuilder(text:myFoos[2]),  //LINE 1003
                      BoxBuilder(text:myFoos[3]),  //LINE 1004
                      BoxBuilder(text:myFoos[4]),  //LINE 1005
                      BoxBuilder(text:myFoos[5]),  //LINE 1006
                    ],
                  ),
                ),
              ),

It seems that the parameters passed are null as I get this result当我得到这个结果时,似乎传递的参数是 null

在此处输入图像描述

If I replace LINE 1100 from BoxBuilder widget class如果我从 BoxBuilder 小部件 class 替换 LINE 1100

            child: Text(
              text==null? 'hello': text,   // LINE 1100
            ),

with just只需

            child: Text(
              text,   // LINE 1100
            ),

I get this error我收到这个错误

   A non-null String must be provided to a Text widget.

called from a LINE 1001, 1002, ... 1006.从 LINE 1001、1002、... 1006 调用。

I am scratching my head here, it's just passing parameters and why do I get a null value?我在这里摸不着头脑,它只是传递参数,为什么我得到一个 null 值?

Since you're using a StatefulWidget then Try widget.text instead.由于您使用的是StatefulWidget ,因此请尝试使用widget.text

child: Text(
  widget.text ?? 'hello',   // LINE 1100
),

and also remove this lines as they are not needed并删除这些行,因为它们不需要

_BoxBuilderState({this.text});

  String text;

There is two problems here这里有两个问题

  1. you re declare variable text, so the new variable value is used which is null您重新声明变量文本,因此使用新的变量值,即 null
  2. since you are using StatefulWidget you use it's parameters by widget.param因为您使用的是StatefulWidget ,所以您通过widget.param使用它的参数

Try this尝试这个

import 'package:flutter/material.dart';

class BoxBuilder extends StatefulWidget {
  BoxBuilder({Key key, @required this.text}): super(key: key);
  String text;

  @override
  _BoxBuilderState createState() => _BoxBuilderState();
}

class _BoxBuilderState extends State<BoxBuilder>  {

  double getSize() {
    if (MediaQuery.of(context).orientation == Orientation.portrait) {
      return MediaQuery.of(context).size.width - 120;
    } else {
      return MediaQuery.of(context).size.height - 120;
    }
  }

  @override
  Widget build(BuildContext context) {
    double _width = getSize() / 2;
    double _height = getSize() / 3;
    return Container(
      width: _width,
      height: _height,
      child: Card(
        color: Colors.white,
        child: Center(
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Text(
              widget.text ?? 'hello',
            ),
          ),
        ),
      ),
    );
  }
}

Note: dart provide better way to check null data注意:dart 提供了更好的方法来检查 null 数据

widget.text ?? 'hello',

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM