Flutter Flatter Future build装载时不显示容器
我正在尝试在我的应用程序中使用futurebuild,但在加载功能后,它仍然显示加载指示器,而不显示容器 这是我的密码Flutter Flatter Future build装载时不显示容器,flutter,dart,Flutter,Dart,我正在尝试在我的应用程序中使用futurebuild,但在加载功能后,它仍然显示加载指示器,而不显示容器 这是我的密码 class _MyHomePageState extends State<MyHomePage> { bool showApp = false; var _questions = new List<Questions>(); _getQuestions() { API.getUsers().then((response) {
class _MyHomePageState extends State<MyHomePage> {
bool showApp = false;
var _questions = new List<Questions>();
_getQuestions() {
API.getUsers().then((response) {
setState(() {
Iterable list = json.decode(response.body);
print(list);
print(list);
_questions = list.map((model) => Questions.fromJson(model)).toList();
print(_questions);
showApp = true;
});
});
}
int index = 0;
bool shouldShow = false;
@override
Widget build(BuildContext context) {
int size = _questions?.length;
void nextQuestion() {
if (index < size - 1)
setState(() {
index++;
});
print(index);
}
double percentage1Calculate() {
int wouldClick = 12;
int ratherClick = 13;
double percentage1 = wouldClick / (wouldClick + ratherClick) * 100;
return percentage1;
}
double percentage2Calculate() {
int wouldClick = 2;
int ratherClick = 3;
double percentage2 = ratherClick / (wouldClick + ratherClick) * 100;
return percentage2;
}
void percengtageTrigger(){
setState(() {
shouldShow = true;
});
Timer timer = Timer(Duration(milliseconds: 1350), () {
setState(() {
shouldShow = false;
});
});
}
final PrimaryColor = const Color(0xff404040);
final PreferredSizeWidget appBar = AppBar(
centerTitle: true,
title: Text(
'Would you Rather',
style: TextStyle(fontFamily: 'FredokaOne'),
),
backgroundColor: PrimaryColor,
);
double stackHeight = (MediaQuery.of(context).size.height -
appBar.preferredSize.height -
MediaQuery.of(context).padding.top);
double stackWidth = MediaQuery.of(context).size.width;
return FutureBuilder(
initialData:null, //initial default data if you have some
future: _getQuestions(),
builder:(BuildContext context, AsyncSnapshot<String> snapshot){
if(snapshot.hasData){
print("Data found, You can continue");
return Center(
child: Container(
child: Text('data avail'),
),
);
}
else if (snapshot.hasError) {
return Center(
child: Container(
child: Text('error'),
),
);
}
else{
print("loading");
return CircularProgressIndicator();
}
}
);
}
}
使用以下命令:
class _MyHomePageState extends State<MyHomePage> {
bool showApp = false;
Future myQuestionsFuture;
var _questions = new List<Questions>();
@override
void initState() {
myQuestionsFuture = _getQuestions();
super.initState();
}
Future<List<Questions>> _getQuestions() async {
final response = await API.getUsers();
Iterable list = json.decode(response.body);
print(list);
print(list);
_questions = list.map((model) => Questions.fromJson(model)).toList();
print(_questions);
setState(() {
showApp = true;
});
return _questions;
}
int index = 0;
bool shouldShow = false;
@override
Widget build(BuildContext context) {
int size = _questions?.length;
void nextQuestion() {
if (index < size - 1)
setState(() {
index++;
});
print(index);
}
double percentage1Calculate() {
int wouldClick = 12;
int ratherClick = 13;
double percentage1 = wouldClick / (wouldClick + ratherClick) * 100;
return percentage1;
}
double percentage2Calculate() {
int wouldClick = 2;
int ratherClick = 3;
double percentage2 = ratherClick / (wouldClick + ratherClick) * 100;
return percentage2;
}
void percengtageTrigger() {
setState(() {
shouldShow = true;
});
Timer timer = Timer(Duration(milliseconds: 1350), () {
setState(() {
shouldShow = false;
});
});
}
final PrimaryColor = const Color(0xff404040);
final PreferredSizeWidget appBar = AppBar(
centerTitle: true,
title: Text(
'Would you Rather',
style: TextStyle(fontFamily: 'FredokaOne'),
),
backgroundColor: PrimaryColor,
);
double stackHeight = (MediaQuery.of(context).size.height -
appBar.preferredSize.height -
MediaQuery.of(context).padding.top);
double stackWidth = MediaQuery.of(context).size.width;
return FutureBuilder<List<Questions>>(
initialData: null, //initial default data if you have some
future:myQuestionsFuture,
builder: (BuildContext context, AsyncSnapshot<List<Questions>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print("Data found, You can continue");
return Center(
child: Container(
child: Text('data avail'),
),
);
} else if (snapshot.hasError) {
return Center(
child: Container(
child: Text('error'),
),
);
}
} else {
print("loading");
return CircularProgressIndicator();
}
});
}
}
class\u MyHomePageState扩展状态{
bool showApp=false;
未来我的问题未来;
var_questions=新列表();
@凌驾
void initState(){
MyQuestions未来=_getQuestions();
super.initState();
}
Future\u getQuestions()异步{
最终响应=等待API.getUsers();
Iterable list=json.decode(response.body);
打印(列表);
打印(列表);
_questions=list.map((model)=>questions.fromJson(model)).toList();
打印问题;
设置状态(){
showApp=true;
});
返回问题;
}
int指数=0;
bool shouldShow=false;
@凌驾
小部件构建(构建上下文){
整数大小=_个问题?.length;
void nextQuestion(){
如果(索引<大小-1)
设置状态(){
索引++;
});
打印(索引);
}
双百分比1计算(){
int-wouldClick=12;
int-ratherClick=13;
双百分比1=wouldClick/(wouldClick+ratherClick)*100;
返回百分比1;
}
双百分比2计算(){
int-wouldClick=2;
int-ratherClick=3;
百分之二=ratherClick/(wouldClick+ratherClick)*100;
返回百分比2;
}
void percengtageTrigger(){
设置状态(){
shouldShow=true;
});
计时器=计时器(持续时间(毫秒:1350),(){
设置状态(){
shouldShow=false;
});
});
}
最终原色=常量颜色(0xff404040);
最终PreferredSizeWidget appBar=appBar(
标题:对,
标题:正文(
“你愿意吗?”,
样式:TextStyle(fontFamily:'FredokaOne'),
),
背景色:原色,
);
double stackHeight=(MediaQuery.of(context).size.height-
appBar.preferredSize.height-
MediaQuery.of(context.padding.top);
double stackWidth=MediaQuery.of(context).size.width;
回归未来建设者(
initialData:null,//如果有初始默认数据
未来:我的问题未来,
生成器:(BuildContext上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
if(snapshot.hasData){
打印(“找到数据,可以继续”);
返回中心(
子:容器(
子项:文本(“数据可用”),
),
);
}else if(snapshot.hasrerror){
返回中心(
子:容器(
子项:文本('error'),
),
);
}
}否则{
打印(“加载”);
返回循环ProgressIndicator();
}
});
}
}
Its显示此错误类型“Future”不是“Future”类型的子类型
class _MyHomePageState extends State<MyHomePage> {
bool showApp = false;
Future myQuestionsFuture;
var _questions = new List<Questions>();
@override
void initState() {
myQuestionsFuture = _getQuestions();
super.initState();
}
Future<List<Questions>> _getQuestions() async {
final response = await API.getUsers();
Iterable list = json.decode(response.body);
print(list);
print(list);
_questions = list.map((model) => Questions.fromJson(model)).toList();
print(_questions);
setState(() {
showApp = true;
});
return _questions;
}
int index = 0;
bool shouldShow = false;
@override
Widget build(BuildContext context) {
int size = _questions?.length;
void nextQuestion() {
if (index < size - 1)
setState(() {
index++;
});
print(index);
}
double percentage1Calculate() {
int wouldClick = 12;
int ratherClick = 13;
double percentage1 = wouldClick / (wouldClick + ratherClick) * 100;
return percentage1;
}
double percentage2Calculate() {
int wouldClick = 2;
int ratherClick = 3;
double percentage2 = ratherClick / (wouldClick + ratherClick) * 100;
return percentage2;
}
void percengtageTrigger() {
setState(() {
shouldShow = true;
});
Timer timer = Timer(Duration(milliseconds: 1350), () {
setState(() {
shouldShow = false;
});
});
}
final PrimaryColor = const Color(0xff404040);
final PreferredSizeWidget appBar = AppBar(
centerTitle: true,
title: Text(
'Would you Rather',
style: TextStyle(fontFamily: 'FredokaOne'),
),
backgroundColor: PrimaryColor,
);
double stackHeight = (MediaQuery.of(context).size.height -
appBar.preferredSize.height -
MediaQuery.of(context).padding.top);
double stackWidth = MediaQuery.of(context).size.width;
return FutureBuilder<List<Questions>>(
initialData: null, //initial default data if you have some
future:myQuestionsFuture,
builder: (BuildContext context, AsyncSnapshot<List<Questions>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print("Data found, You can continue");
return Center(
child: Container(
child: Text('data avail'),
),
);
} else if (snapshot.hasError) {
return Center(
child: Container(
child: Text('error'),
),
);
}
} else {
print("loading");
return CircularProgressIndicator();
}
});
}
}