如何从一页转到另一页?

时间:2019-08-08 00:13:52

标签: flutter dart

我有我的主页,当我按Enter键时,将打开另一页,然后在中间一个按钮中,当按下该按钮时,该功能将返回主页。但是当我按下它时,出现此错误:

  

错误:“ package:flutter / src / widgets / text.dart”:失败的断言:第253行   pos 10:“数据!= null”:必须为文本提供非null的字符串   小部件。带有星号的线是错误线。

import 'package:flutter/material.dart';

import './SecondPage.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'BelaBlock',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.cyan,

      ),
      home: MyHomePage(title: "Bela Block"),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  //Inputs 
  var _counter = "0";
  var _counter1 = "0";
  var text1;

  //Select tim
  var tim1 = 0;
  var tim2 = 0;

  //Contacts
  String contacts = "";

  //Points
  var count1 = 0;
  var count2 = 0;
  var input1 = 0;
  var input2 = 0;
  var cons = 162;
  var t1 = 0;
  var t2 = 0;

   @override
  void dispose() {
    tim1 = 0;
    tim2 = 0;
    contacts = "";

    super.dispose();
  }

  void contact1 (){
    setState(() {
      tim1 = 1;
      tim2 = 0;
    });
  }

  void contact2 (){
    setState(() {
      tim2 = 1;
      tim1 = 0;
    });
  }

  void clear (){
    setState(() {
      _counter = "0";
      _counter1 = "0";
      contacts = "";
      tim1 = 0;
      tim2 = 0;
    });
  }

  void fall(){
    if (contacts == "Tim1") {
      if (t1 <= t2){
        input1 = 0;
        input2 = 162;
        setState(() {
          _counter = 0.toString();
          _counter1 = 162.toString();
        });
        print("Tim 1 je pao.");
      }
      else{
        print("Tim 1 je prosao.");
      }
    }
    if (contacts == "Tim2") {
      if (t2 <= t1){
        input1 = 162;
        input2 = 0;
        setState(() {
          _counter = 162.toString();
          _counter1 = 0.toString();
        });
        print("Tim 2 je pao.");
      }
      else{
        print("Tim 2 je prosao.");
      }
    }

  }


  void _incrementCounter(String buttonText) {

    if (tim1 == 1){
      setState(() {
      _counter = _counter + buttonText.toString(); 
      count2 = int.parse(_counter);
      print(count2);
      input2 = cons - count2;
      t1 = count2;
      t2 = input2;
      _counter1 = input2.toString();
    });
    }

    if(tim2 == 1){
      setState(() {
      _counter1 = _counter1 + buttonText.toString();
      count1 = int.parse(_counter1);
      print(count1);
      input1 = cons - count1;
      t2 = count1;
      t1 = input1;
      _counter = input1.toString();
    });
    }

  }

  Widget buildButton (String buttonText){
    return new Expanded(
      child: new MaterialButton(
        color: Colors.black,
        padding: new EdgeInsets.all(20),
        child: new Text(buttonText,
          style: TextStyle(
            backgroundColor: Colors.black,
            color: Colors.white,
            fontSize: 24,
            fontWeight: FontWeight.bold,
          ),),

        onPressed: () => _incrementCounter(buttonText)
      ),
    );
  }
  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),

      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          children: <Widget>[

            new Row(children: [
             Expanded(
                child: new MaterialButton(
                color: Colors.black,
                textColor: Colors.white,
                padding: new EdgeInsets.only(left: 20, right: 100), 
              child: new Text("Tim 1",
              style: TextStyle(
                color: Colors.white,
                fontSize: 20,
                fontWeight: FontWeight.bold),),
                onPressed: () {
                  contacts = "Tim1";
                  fall();}
              ),
             ),
              Expanded(child: new MaterialButton(
                color: Colors.black,
                padding: new EdgeInsets.only(left: 100, right: 20), 
              child: new Text("Tim 2",
              style: TextStyle(
                color: Colors.white,
                fontSize: 20,
                fontWeight: FontWeight.bold),),
                onPressed: () {
                  contacts = "Tim2";
                  fall();
                }
              ),
              ),
            ],
              ),


            new Row(children: [
              Expanded(
                child: new GestureDetector(
                onTap: (contact1),
                child: text1 = Text(_counter, maxLines: 1,
                textAlign: TextAlign.left, style: TextStyle(
                fontSize: 50,
                fontWeight: FontWeight.bold,
              ),),
              ),
              ),
              Expanded (
                child: new GestureDetector(
                onTap: (contact2),
                child: Text(_counter1, maxLines: 1,
                **textAlign: TextAlign.right, style: TextStyle(**
                fontSize: 50,
                fontWeight: FontWeight.bold)
                ),
              ),
              ),
            ],
            ),

            new Row(
              children: <Widget>[
                Expanded(
                  child: new MaterialButton(
                    padding: EdgeInsets.only(left: 15,right: 15, bottom: 25, top: 18),
                    color: Colors.black,
                    textColor: Colors.white,
                    onPressed: () {},
                    child: Text("Points", 
                    style: TextStyle(
                      fontSize: 40,
                      fontWeight: FontWeight.bold,
                    ),),
                  ),
                ),
                Expanded(
                  child: new MaterialButton(
                    padding: EdgeInsets.only(left: 15,right: 15, bottom: 26, top: 23),
                    color: Colors.black,
                    textColor: Colors.white,
                    onPressed: () {},
                    child: Text("Contacts", textAlign: TextAlign.end, 
                    style: TextStyle(
                      fontSize: 35,
                      fontWeight: FontWeight.bold,
                    ),),
                  ),
                )
              ],
            ),

            new Row(children: [
              buildButton("1"),
              buildButton("2"),
              buildButton("3"),
            ],
            ),
            new Row(children: [
              buildButton("4"),
              buildButton("5"),
              buildButton("6"),
            ],
            ),
            new Row(children: [
              buildButton("7"),
              buildButton("8"),
              buildButton("9"),
            ],
            ),
            new Row(children: [
              buildButton(">"),
              buildButton("0"),
              new Expanded(
                child: new MaterialButton(
                  color: Colors.black,
                  textColor: Colors.white,
                  padding: new EdgeInsets.all(20),
                  child: new Text("<",
                      style: TextStyle(
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                      ),),
                  onPressed: () => clear(),
              ),
            ),
            ],
            ),

            new Row(children: [
              new Expanded(
                child: new MaterialButton(
                  padding: new EdgeInsets.all(10),
                  color: Colors.cyan,
                  child: new Text("ENTER",
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 48,
                        fontWeight: FontWeight.bold,
                      ),),
                  onPressed: () {
                   Navigator.push(context,
                        MaterialPageRoute(builder: (context) => FirstPage()),
                    );
                  }
              ),
            ),
            ],
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

3 个答案:

答案 0 :(得分:1)

如果您确定要从该行中获取例外,只需将其替换为Text(_counter1 ?? ‘’),以确保它将包含数据。无论如何,通过查看您的代码,我看不到_counter1可能被分配给null的位置,而且,将其用作var不会产生影响,因为它将隐式转换为String

答案 1 :(得分:0)

点击“ Enter”后,我可以导航到另一个页面。我认为这是被执行的行为。 最近,我在标准的小部件/代码中遇到了类似的问题。我要做的就是运行flutter clean,关闭IDE,然后重新打开。如果仍然不能解决问题,请再次运行flutter clean并重新启动计算机。

答案 2 :(得分:0)

异常来自文本小部件,给您的行号是Text类内的行号,它检查数据是否为空。

您只需要仔细检查Text()小部件是否获得了不为空的数据。尽管您可以这样使用??运算符来完成此操作:Text(data ?? "")(如果data为空,它将用空字符串替换data)找出null的来源,并正确处理数据源。

还有另一个问题。似乎您是在将FirstPage()推入导航器而不导入实际的类。