Flutter Future构建在加载时未显示容器

时间:2020-05-06 10:59:51

标签: flutter dart

我正在尝试在我的应用程序中使用futurebuild,但是在加载该函数后,它仍显示未显示容器的指示符

这是我的代码

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();
           }
        }
    );
  }
}

我将来会使用_getQuestions,但这只是打印函数的值。没有显示snapshot.hasData的容器

不知道该函数为什么要打印数据,但是在FutureBuilder中它不会更改其状态:(

模型问题

import 'package:flutter/material.dart';


class Questions {
  String would;
  String rather;
  int wouldClick;
  int ratherClick;

  Questions(int wouldClick, int ratherClick, String would, String rather) {
    this.wouldClick = wouldClick;
    this.ratherClick = ratherClick;
    this.would = would;
    this.rather = rather;
  }

  Questions.fromJson(Map json)
      : wouldClick = json['wouldClick'],
        ratherClick = json['ratherClick'],
        would = json['would'],
        rather = json['rather'];

  Map toJson() {
    return {'wouldClick': wouldClick, 'ratherClick': ratherClick, 'would': would, 'rather': rather};
  }
  @override
  String toString() {
    return "{would: $would, rather: $rather, wouldClick: $wouldClick, ratherClick: $ratherClick}";
  }
}

1 个答案:

答案 0 :(得分:1)

使用此:

$ awk -f tst.awk file
P3
# feep.ppm
4 4
15
0 0 0
0 0 0
0 0 0
15 0 15

P3
# feep.ppm
4 4
15
0 0 0
0 15 7
0 0 0
0 0 0

P3
# feep.ppm
4 4
15
0 0 0
0 0 0
0 15 7
0 0 0

P3
# feep.ppm
4 4
15
15 0 15
0 0 0
0 0 0
0 0 0