'future <dynamic>'类型不是'function'类型的子类型

时间:2019-08-26 15:26:32

标签: flutter future

当我在调试模式下运行我的应用程序时,它在屏幕上以及调试控制台中均显示错误“类型'future'不是类型'功能'的子类型”错误。有人能帮我吗?我想这是异步函数“重置”,“ rateoGet”和“ rateoSave”的问题,但我找不到任何解决方案。 附言我删除了部分代码,因为它对这个问题没有用。

int plus;
int min;
int per;
int div;
double val; 
int gameswon =0;
int moves;
static int mosse=15;
String win = "gioca";
int games=0;
double rateo=1;
String mode;
int flag;
var timer=30;


@override
void initState() {
    super.initState();
    reset();
}

@override
Widget build(BuildContext context) {
    return Scaffold(
        body: 
            MyButton(text: "$per" ,color: Colors.deepPurpleAccent, onTap: (){
                setState(() {
                    val*=per;
                });
                if(widget.mode=="timermode" && flag==0){
                    timerceckresults();
                }else if(widget.mode=="movesmode"){
                    checkResult();
                }
            },

            MyBottomButton(text: "Reset",color: Colors.indigo,width:160, onTap: reset()),

    ),
}
checkResult() {
    if(val == 101) {
        print("hai vinto");
        win="Hai Vinto";
        setState(() {});
        gameswon++;
        Timer(Duration(seconds: 2), () {
            reset();
    });
    } else {
        print("ci sei quasi");
        moves++;
        mosse--;
        win="$mosse moves left";
        setState(() {});
        if(moves>14){
            print("hai perso coglione");
            win="Hai Perso Coglione";
            setState(() {});
            Timer(Duration(seconds: 2), () {
                reset();
            });
        }
    }
}
timerceckresults(){
    flag=1;
    timer = 30;
    Timer.periodic(Duration(seconds: 1), (t){
        timer--;
        setState(() {
            win = "${timer.toString()}seconds left";
        });
    if(val==101){
        timer=0;
    }
    if(timer == 0) {
        t.cancel();
        if(val == 101) {
            win="Hai Vinto";
            setState(() {});
            gameswon++;
            Timer(Duration(seconds: 2), () {
                reset();
            });
        } else {
            win="Hai Perso Coglione";
            setState(() {});
            Timer(Duration(seconds: 2), () {
                reset();
            });
        }
    }
});
static int randNum(x,y) {
    var rng = new Random();
    return rng.nextInt(y-x)+x;
}
reset() async{
    timer=1;
    plus = randNum(4, 9);
    min = randNum(5, 19);
    per = randNum(3, 9);
    div = randNum(2, 5);
    val = randNum(2, 11).toDouble();
    moves = 0;
    mosse=15;
    if(widget.mode=="timermode"){
        win="start playing";
    }else{
        win="$mosse moves left";
    }
    await rateoSave();
    await rateoGet();
    games++;
    rateo=gameswon/(games-1);
    await rateoSave();
    flag=0; 
    setState(() {});
}
rateoSave() async {
    SharedPreferences prefs=await SharedPreferences.getInstance();
    await prefs.setInt("games",games);
    await prefs.setInt("gameswon",gameswon);
}
rateoGet() async {
    SharedPreferences prefs=await SharedPreferences.getInstance();
    games=(prefs.getInt("games") ?? 0);
    gameswon=(prefs.getInt("gameswon") ?? 0);

3 个答案:

答案 0 :(得分:0)

https://dart.dev/codelabs/async-await在阅读答案之前先阅读此书,将帮助您很多

reset() async{
    timer=1;
    plus = randNum(4, 9);
    min = randNum(5, 19);
    per = randNum(3, 9);
    div = randNum(2, 5);
    val = randNum(2, 11).toDouble();
    moves = 0;
    mosse=15;
    if(widget.mode=="timermode"){
        win="start playing";
    }else{
        win="$mosse moves left";
    }
    await rateoSave();
    await rateoGet();
    games++;
    rateo=gameswon/(games-1);
    await rateoSave();
    flag=0; 
    setState(() {});
}
Future<bool> rateoSave() {
    SharedPreferences prefs= SharedPreferences.getInstance();
    prefs.setInt("games",games);
    prefs.setInt("gameswon",gameswon);
return true;
}

Future<bool> rateoGet() async {
    SharedPreferences prefs= SharedPreferences.getInstance();
    await  games=(prefs.getInt("games") ?? 0);
    await  gameswon=(prefs.getInt("gameswon") ?? 0);
    return true;
  }

答案 1 :(得分:0)

您正在尝试从返回未来的方法中获取变量。您需要在调用该函数之前添加await。

您能告诉我们此错误发生在哪一行?

答案 2 :(得分:0)

要记住的最重要的事情是,如果呼叫链中的任何内容返回了期货,则其上方的所有内容必须通过返回期货来处理期货。未来本身(如果不必执行任何处理),或者await处理并处理返回的值(但您仍将返回未来)。