flutter setState,为什么第一个输出为空,我如何跳过输出直到它不为空?

flutter setState, why the first output is null and how can i skip the output until it isn't null?

Answers.json

{
    "hello-world3-2": "19"
}

My game screen codes

String userAnswer = '';
var decodedAnswer;

class GameScreen extends StatefulWidget {
  @override
  _GameScreenState createState() => _GameScreenState();
}

class _GameScreenState extends State<GameScreen> {

这里我从 json 文件中得到答案 19

  getDatas() async {
    final realAnswer = await rootBundle.loadString('lib/assets/answers.json');
    final decodedText = await json.decode(realAnswer.toString());
    decodedAnswer = decodedText["hello-world3-2"];

我想在 decodedAnswer 更改其值后全局生成它

    setState(
      () {
        while (decodedAnswer == null) {
          decodedAnswer = decodedText["hello-world3-2"];

再次循环以防解码后的答案仍然为空

          continue;
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    getDatas();
    print(decodedAnswer.toString());

Terminal output

Performing hot restart...                                               

Restarted application in 2,381ms.
W/Settings(25117): Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
V/HiTouch_HiTouchSensor(25117): User setup is finished.
V/AudioManager(25117): playSoundEffect   effectType: 0
V/AudioManager(25117): querySoundEffectsEnabled...
I/flutter (25117): null
I/flutter (25117): 19
I/flutter (25117): 19
I/flutter (25117): 19
I/flutter (25117): 19
I/flutter (25117): 19

重构您的函数以获得类似于

的数据
Future<String> getDatas() async {
//---- Fetch data from file.
return "your return value";}

并使用它

getDatas().then((value){
  //--- Perform update here. 
  //--- setState or whatever
  setState(() {
    decodedAnswer = decodedText["hello-world3-2"];
  });
  print("Value :$value");
});

您正在迫切地考虑这个问题。 Flutter 是一个声明式框架,这意味着您真的不想像使用一段 C 代码那样逐步控制您的应用程序流程。 setState 方法更改 StatefulWidget 的状态并使其再次变为 build。您不想在那里循环并不断检查值是否已更改 - 这只会导致您的代码冻结。

不要将此视为:1) 创建小部件,2) 构建小部件,3) 检查数据更新 4) 如果数据未更新,重复 3)

您想这样考虑:1) 创建一个小部件,2) 启动数据检索过程,完成后更改状态,3) 构建我的小部件并让它确定何时重建.

因此,您可以改为执行以下操作:

class _GameScreenState extends State<GameScreen> {
  String _data;

  @override
  void initState() {
    super.initState();
    _data = '';
    getData().then((value) {
      setState(() {
        _data = value;
      });
    });
  }

  Future<String> getData() async {
    //do your processing
    return decodedText;
  }

  @override
  Widget build(BuildContext context) {
    pritn(_data);
    return Text(_data);
  }
}