简体   繁体   English

Dart Flutter:Function 未执行

[英]Dart Flutter: Function not executing

I am new to Dart & Flutter, I am trying to test Future functionality.我是 Dart 和 Flutter 的新手,我正在尝试测试Future的功能。 I wrote a short async function, in which two Future objects are nested.我写了一个简短的async function,其中嵌套了两个Future对象。 Problem: The function does not execute, and when I try to attribute it to a variable I get an error This expression has a type of 'void' so its value can't be used.问题: function 未执行,当我尝试将其归因于变量时,我收到错误This expression has a type of 'void' so its value can't be used. and Only static members can be accessed in initializers.并且Only static members can be accessed in initializers. . . Here is the code: [![enter image description here][1]][1]这是代码:[![在此处输入图像描述][1]][1]

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

class _ChoseLocationState extends State<ChoseLocation> {
  int counter = 0;
  
  void simulateRequest() async {

    // first future holds family name
    String famNameFunc = await Future.delayed(Duration(seconds: 2), (){
      String famName = 'Shanshi';
      return famName;
    });


    // second future holds first name
    String compName = await Future.delayed(Duration(seconds: 1), (){
      String fstName = 'Yoshi';
      String compName = '$fstName - $famNameFunc';
      return compName;
    });

    print(compName);
  }

  dynamic funcex = simulateRequest();

  @override
  void initState() {
    super.initState();
    print('This is the initial state.');
  }
  
  
  @override
  Widget build(BuildContext context) {
    print('This is the build function processing.');
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red,
          title: Text('Set Location'),
          centerTitle: true,
          ),
        body: RaisedButton(
          onPressed: (){
          setState(() {
            counter++;
          });
        },
          color: Colors.blue,
          child: Text('Counter is: $counter'),
        ),
      );
  }
}```

[1]: https://i.stack.imgur.com/XmvY9.png

Try the following:尝试以下操作:

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

  final String title;

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

class _ChoseLocationState extends State<ChoseLocation> {
  int counter = 0;
  
  void simulateRequest() async {

    // first future holds family name
    String famNameFunc = await Future.delayed(Duration(seconds: 2), (){
      String famName = 'Shanshi';
      return famName;
    });


    // second future holds first name
    String compName = await Future.delayed(Duration(seconds: 1), (){
      String fstName = 'Yoshi';
      String compName = '$fstName - $famNameFunc';
      return compName;
    });

    print(compName);
  }

//   dynamic funcex = simulateRequest();

  @override
  void initState() {
    super.initState();
    simulateRequest();
    print('This is the initial state.');
  }
  
  
  @override
  Widget build(BuildContext context) {
    print('This is the build function processing.');
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red,
          title: Text('Set Location'),
          centerTitle: true,
          ),
        body: RaisedButton(
          onPressed: (){
          setState(() {
            counter++;
          });
        },
          color: Colors.blue,
          child: Text('Counter is: $counter'),
        ),
      );
  }
}

You need to call simulateRequest() inside a method example initState , and since it doesnt return anything then you cannot assign it to a variable.您需要在方法示例initState中调用simulateRequest() ,并且由于它不返回任何内容,因此您不能将其分配给变量。

simulateRequest has a return type of void , so if you'r trying to store this function in the variable you shouldn't put parenthesis. simulateRequest请求的返回类型为void ,因此如果您尝试将此 function 存储在变量中,则不应放置括号。 If you use the parenthesis you'll be running the function and assigning it's returned value which is void to the variable funcex and thats why u're getting: This expression has a type of 'void' so its value can't be used.如果你使用括号,你将运行 function 并将它的返回值分配给变量funcex ,这就是你得到的原因:这个表达式的类型是“ void This expression has a type of 'void' so its value can't be used.

      @override
  void initState() {
    dynamic funcex = simulateRequest;
    funcex();
    super.initState();
    print('This is the initial state.');
  }

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

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