【问题标题】:The following ArgumentError was thrown building GamePage(dirty, state: _GamePageState#d2e81): Invalid argument(s) on flutter在构建 GamePage(dirty, state: _GamePageState#d2e81): Invalid argument(s) on flutter 时抛出了以下 ArgumentError
【发布时间】:2020-08-09 13:46:52
【问题描述】:

我收到此错误,不知道为什么。你能帮助我吗? 我正在尝试通过用户选择在应用程序中使用不同的列表。但是当我启动应用程序时出现此错误

这里是主文件

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'story_brain.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';

//pride or insult

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: GamePage(),
    );
  }
}

class FirstScreen extends StatelessWidget {
  Widget build(BuildContext context2) {
    return MaterialApp(
      home: FirstScreenMaterialApp(),
    );
  }
}

class FirstScreenMaterialApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.purple[900],
      body: Container(
        child: Padding(
          padding: const EdgeInsets.fromLTRB(20, 10, 20, 50),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Image(
                image: AssetImage("assets/images/logo.png"),
                width: 000,
                height: 400,
              ),
              Expanded(
                child: FlatButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => GamePage()),
                    );
                  },
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15)),
                  color: Colors.lightBlue,
                  child: Center(
                    child: Text(
                      "Klasik",
                      style: TextStyle(fontSize: 40),
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Expanded(
                child: FlatButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => GamePage()),
                    );
                  },
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15)),
                  color: Colors.red,
                  child: Center(
                    child: Text(
                      "Temel içgüdü(+18)",
                      style: TextStyle(fontSize: 40),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class GamePage extends StatefulWidget {
  _GamePageState createState() => _GamePageState();
}

class _GamePageState extends State<GamePage> {
  StoryBrain storyBrain = StoryBrain();
  Widget build(BuildContext context) {
    return Center(
      child: Scaffold(
        body: Container(
          color: Colors.purple[800],
          padding: EdgeInsets.symmetric(vertical: 50.0, horizontal: 30.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Expanded(
                flex: 20,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      flex: 0,
                      child: Icon(
                        Icons.whatshot,
                        size: 70,
                        color: storyBrain.changeHeatBarColor(),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Text(
                        storyBrain.updateHeat(),
                        style: TextStyle(
                          fontSize: 50,
                          fontFamily: "CustomFamilyName",
                          color: storyBrain.changeHeatBarColor(),
                        ),
                      ),
                    )
                  ],
                ),
              ),
              Center(
                child: Expanded(
                  flex: 100,
                  child: Center(
                    child: Container(
                      padding: EdgeInsets.all(20),
                      margin: EdgeInsets.fromLTRB(20, 0, 20, 60),
                      width: 300,
                      height: 250,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        color: storyBrain.setColor(),
                      ),
                      child: Text(
                        storyBrain.getText(),
                        style: TextStyle(
                          fontSize: 30.0,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 10,
                child: FlatButton(
                  onPressed: () {
                    storyBrain.changeNumber();

                    setState(() {
                      storyBrain.getText();
                      storyBrain.setColor();
                      storyBrain.changeHeatBarColor();
                      storyBrain.updateHeat();
                    });
                  },
                  color: storyBrain.setColor(),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(15),
                  ),
                  child: Center(
                    child: Text(
                      "Değiştir",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 30.0,
                      ),
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              )
            ],
          ),
        ),
      ),
    );
  }
}

story_brain 文件:

import 'package:flutter/material.dart';
import 'story.dart';
import 'dart:math';

class StoryBrain {
  static Random random = Random();
  int textNumber;
  bool gameTypeIsClasic = false;

  static List<Story> textListClasic = [
    Story(
      "text content 2",
      "göm",
    ),
    Story(
      "text content 1",
      "öv",
    ),
    Story(
      "text content 3",
      "göm",
    ),
    Story("text content 4", "öv")
  ];

  static List<Story> textListTemel = [
    Story(
      "text content 1",
      "göm",
    ),
    Story(
      "text content 2",
      "öv",
    ),
    Story(
      "text content 3",
      "göm",
    ),
    Story("text content 4", "öv")
  ];

  int heat = 1;

  StoryBrain() {
    if (gameTypeIsClasic == true) {
      random.nextInt(textListClasic.length - 1);
    } else {
      random.nextInt(textListTemel.length - 1);
    }
  }

  void changeNumber() {
    if (gameTypeIsClasic == true) {
      textNumber = random.nextInt(textListClasic.length - 1);
    } else {
      textNumber = random.nextInt(textListTemel.length - 1);
    }
  }

  String getText() {
    if (gameTypeIsClasic == true) {
      return textListClasic[textNumber].textContent;
    } else {
      return textListTemel[textNumber].textContent;
    }
  }

  String getTextType() {
    if (gameTypeIsClasic == true) {
      return textListClasic[textNumber].textType;
    } else {
      return textListTemel[textNumber].textType;
    }
  }

  Color setColor() {
    if (getTextType() == "öv") {
      return Colors.blue;
    } else {
      return Colors.red;
    }
  }

  String updateHeat() {
    if (getTextType() == "öv") {
      if (heat > 1 && heat < 20) {
        heat = heat - 1;
      } else if (heat > 20 && heat < 100) {
        heat -= 10;
      } else if (heat > 200 && heat < 500) {
        heat -= 25;
      } else if (heat > 500) {
        heat = (heat ~/ 3);
      } else {
        heat = heat;
      }
    } else {
      if (heat < 1000) {
        heat = heat * 2;
      } else {
        heat += 100;
      }
    }
    return heat.toString() + "x";
  }

  Color changeHeatBarColor() {
    if (heat >= 50 && heat < 100) {
      return Colors.orange;
    } else if (heat >= 100 && heat < 500) {
      return Colors.redAccent;
    } else if (heat > 500) {
      return Colors.red[800];
    } else {
      return Colors.blue;
    }
  }
}

【问题讨论】:

  • 如果提供的答案解决了您的问题,请告诉我@glorry

标签: android flutter dart


【解决方案1】:

您收到错误是因为您没有给 textNumber 一个初始值,并且您在其他方法定义中使用它来访问您的 textListClasic 列表中的 elements

我更新了StoryBrain 类来为textNumber 变量设置一个初始值:

class StoryBrain {
  static Random random = Random();
  // initialize the textNumber variable 
  int textNumber = 0; // new line
  bool gameTypeIsClasic = false;

  static List<Story> textListClasic = [
    Story(
      "text content 2",
      "göm",
    ),
    Story(
      "text content 1",
      "öv",
    ),
    Story(
      "text content 3",
      "göm",
    ),
    Story("text content 4", "öv")
  ];

 ... // remaining codes 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2019-06-11
    • 2019-07-02
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多