【发布时间】:2020-06-26 08:29:42
【问题描述】:
说明:
在我的主页上,每个用户输入他们的昵称并单击按钮开始游戏。然后每个玩家必须回答多项选择,最后显示分数。
问题 当我调用“initQuestion()”时,我无法获取结果的值,并且出现一些错误,例如“此表达式的类型为 'void',无法使用。”或“方法驱动被调用为空”
代码 我的主页调用 Game() 函数:
import 'package:flutter/material.dart';
import 'package:trivia/src/screens/questionpage.dart';
class Game extends StatelessWidget {
final List<String> _playerNameList;
final String gameMode; // Easy, Normal, Hard defined on HomePage
Game(this.gameMode, this._playerNameList);
Widget build(BuildContext context) {
//variables
int round = 3;
int playerNumber = _playerNameList.length;
Map<String, int> score; //List to store score like {'PlayerName1': '0', 'PlayerName2": '0', ...};
//Function
for (int i = 0; i < round; i++) { //for each round
for (int j = 0; j < playerNumber; j++) { //for each player
int result = initQuestion(context, gameMode, _playerNameList[j]); // Run "Question game"
score[_playerNameList[j]] = score[_playerNameList[j]]+result; // Get result (1 if answer correct; 0 if answer invalid) and store score
}
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Game end'),
),
body: Center(
child: Text('The winner is ...'),
),
),
);
}
}
initQuestion(context, gameMode, playerName) async {
//Question
String question = "In which continent is France located?";
List<String> _choice = <String>["Europe","Asia","Africa"];
String answer = "Europe";
final result = await Navigator.push(context, MaterialPageRoute(builder: (context) => QuestionPage(playerName: playerName, answer: answer, choice: _choice, question: question)),
);
//Get result (0 or 1)
return result; //--> Edited thx Selim Kundakçıoğlu
}
【问题讨论】: