Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 状态完整小部件不更改数据,如何修复?_Flutter_Dart - Fatal编程技术网

Flutter 状态完整小部件不更改数据,如何修复?

Flutter 状态完整小部件不更改数据,如何修复?,flutter,dart,Flutter,Dart,我有一个文本字段,它从一个函数(getCurrentAssetsData)获取其部分数据,该函数从数据库(sqlite)获取数据 但它显示的是null,而不是数据。 由于我能够打印报表,因此数据正确。 我的代码: class CoinList extends StatefulWidget { @override _CoinListState createState() => _CoinListState(); } class _CoinListState extends Sta

我有一个文本字段,它从一个函数(getCurrentAssetsData)获取其部分数据,该函数从数据库(sqlite)获取数据

但它显示的是null,而不是数据。 由于我能够打印报表,因此数据正确。

我的代码:

class CoinList extends StatefulWidget {
  @override
  _CoinListState createState() => _CoinListState();
}

class _CoinListState extends State<CoinList> {
  DbHelper helper = DbHelper();
  List<Coin> coins;
  int count=0;
  @override
  Widget build(BuildContext context) {
    if(coins==null){
      coins = List<Coin>();
      getData();
    }
    return Scaffold(
      body: coinListItems(),

      floatingActionButton: FloatingActionButton(
        onPressed: (){
          navigateToDetail(Coin(0,''));
        },
        tooltip: "Add new Todo",
        child: new Icon(Icons.add),
      ),
    );
  }
  ListView coinListItems(){
    return ListView.builder(
      itemCount: count,
      itemBuilder: (BuildContext context,int position){
        return GestureDetector(
          child: SwipeDetector(
            child:Card(
            color: Colors.white,
            elevation: 2.0,
            child: ListTile(
              leading: CircleAvatar(
                backgroundColor: Colors.red,
                child: Text(this.coins[position].id.toString()),
              ),
              title: Text(this.coins[position].coin.toString() + " Temp Data " + getCurrentAssetsData(this.coins[position]).toString()), //title
              subtitle: Text(this.coins[position].description),

              onTap: (){
                //TODO:Open Coin specific orders
                navigateToOrders(this.coins[position]);
              },

            ),
          ),
            onSwipeRight: () {
              print("Swipe Right");//TODO:Delete coin code
            },
          ),
          onDoubleTap: (){
        debugPrint("Tapped on "+this.coins[position].id.toString());
        navigateToDetail(this.coins[position]);
          },
        );
      },
    );
  }
  void getData(){
    final dbFuture = helper.initializeDb();
    dbFuture.then((result){
      final coinFuture = helper.getCoin();
      coinFuture.then(
              (result){
            List<Coin> coins = List<Coin>();
            count = result.length;
            for(int i=0;i<count;i++){
              coins.add(Coin.fromObject(result[i]));
              debugPrint(coins[i].marketValue.toString());//title
            }
            setState(() {
              this.coins = coins;
              count = count;
            });
            debugPrint("Items "+count.toString());
          }
      );
    });
  }

  String getCurrentAssetsData(coin){

    final dbFuture = helper.initializeDb();
    dbFuture.then((result){
      //final coin = helper.retrieveCoin(coin_name);
      final coinOrderFuture = helper.getCoinOrder(coin);
      double totalCost = 0;
      double totalCount = 0;
      double totalCommission=0;
      coinOrderFuture.then(
              (result){
            List<CoinOrder> coinOrders = List<CoinOrder>();
            int count = result.length;
            for(int i=0;i<count;i++){
              coinOrders.add(CoinOrder.fromObject(result[i]));
              totalCost += coinOrders[i].count * coinOrders[i].buyValue;
              totalCount += coinOrders[i].count;
              totalCommission+= coinOrders[i].commission;
            }

            //averagePrice = totalCost/totalCount;
            //totalCommission = totalCommission;
            //totalCost = totalCost;
            String sData = "Avg: ${totalCost/totalCount} Investment: ${totalCost}";
            debugPrint(sData);
            return sData;

          }
      );
    });
  }

  void navigateToDetail(Coin coin) async{
    bool result = await Navigator.push(context, MaterialPageRoute(builder: (context)=>CoinDetail(coin)),);
    if(result==true){
      getData();
    }
  }

  void navigateToOrders(Coin coin) async{
    bool result = await Navigator.push(context, MaterialPageRoute(builder: (context)=>CoinOrderList(coin)),);
    /*if(result==true){
      getOrderData();
    }*/
  }
}
class CoinList扩展了StatefulWidget{
@凌驾
_CoinListState createState()=>\u CoinListState();
}
类_CoinListState扩展了状态{
DbHelper=DbHelper();
列出硬币清单;
整数计数=0;
@凌驾
小部件构建(构建上下文){
如果(硬币==null){
硬币=列表();
getData();
}
返回脚手架(
正文:coinListItems(),
浮动操作按钮:浮动操作按钮(
已按下:(){
导航细节(硬币(0,);
},
工具提示:“添加新待办事项”,
子:新图标(Icons.add),
),
);
}
ListView coinListItems(){
返回ListView.builder(
itemCount:count,
itemBuilder:(构建上下文,int位置){
返回手势检测器(
孩子:SwipeDetector(
孩子:卡片(
颜色:颜色,白色,
标高:2.0,
孩子:ListTile(
领先:CircleAvatar(
背景颜色:Colors.red,
child:Text(this.coins[position].id.toString()),
),
标题:Text(this.coins[position].coin.toString()+“Temp Data”+GetCurrentAssetData(this.coins[position]).toString(),//标题
字幕:文本(此.coins[位置].说明),
onTap:(){
//TODO:开放式硬币特定订单
导航命令(此.硬币[位置]);
},
),
),
对:(){
打印(“向右滑动”);//TODO:删除硬币代码
},
),
onDoubleTap:(){
debugPrint(“点击”+这个.coins[position].id.toString());
导航详细信息(此.硬币[位置]);
},
);
},
);
}
void getData(){
final dbFuture=helper.initializeDb();
dbFuture.then((结果){
final coinFuture=helper.getCoin();
那么(
(结果){
列表硬币=列表();
count=result.length;
对于(inti=0;iCoinOrderList(coin));
/*如果(结果==真){
getOrderData();
}*/
}
}
以及输出:

您可以看到,我可以在控制台中显示数据输出。 但不是在文本小部件中。 由于它是有状态的小部件,我认为它会反映数据。
同样,我在创建文本字段时设置/调用函数。

只要您使用有状态的小部件,就必须调用它才能更新那里的UIHi。您必须发布更多代码。尝试发布整个小部件代码以及
GetCurrentAssetData
函数的实现。提供也可以打印的证明。因为如果您能够打印数据,数据应该是可见的。@Brugui即使是有状态的小部件,OP也在调用函数。除非函数返回未来,否则这不重要,因为他没有在他的状态中存储任何内容。您完全正确,我们需要查看函数我添加了函数部分,你现在能查一下吗
class CoinList extends StatefulWidget {
  @override
  _CoinListState createState() => _CoinListState();
}

class _CoinListState extends State<CoinList> {
  DbHelper helper = DbHelper();
  List<Coin> coins;
  int count=0;
  @override
  Widget build(BuildContext context) {
    if(coins==null){
      coins = List<Coin>();
      getData();
    }
    return Scaffold(
      body: coinListItems(),

      floatingActionButton: FloatingActionButton(
        onPressed: (){
          navigateToDetail(Coin(0,''));
        },
        tooltip: "Add new Todo",
        child: new Icon(Icons.add),
      ),
    );
  }
  ListView coinListItems(){
    return ListView.builder(
      itemCount: count,
      itemBuilder: (BuildContext context,int position){
        return GestureDetector(
          child: SwipeDetector(
            child:Card(
            color: Colors.white,
            elevation: 2.0,
            child: ListTile(
              leading: CircleAvatar(
                backgroundColor: Colors.red,
                child: Text(this.coins[position].id.toString()),
              ),
              title: Text(this.coins[position].coin.toString() + " Temp Data " + getCurrentAssetsData(this.coins[position]).toString()), //title
              subtitle: Text(this.coins[position].description),

              onTap: (){
                //TODO:Open Coin specific orders
                navigateToOrders(this.coins[position]);
              },

            ),
          ),
            onSwipeRight: () {
              print("Swipe Right");//TODO:Delete coin code
            },
          ),
          onDoubleTap: (){
        debugPrint("Tapped on "+this.coins[position].id.toString());
        navigateToDetail(this.coins[position]);
          },
        );
      },
    );
  }
  void getData(){
    final dbFuture = helper.initializeDb();
    dbFuture.then((result){
      final coinFuture = helper.getCoin();
      coinFuture.then(
              (result){
            List<Coin> coins = List<Coin>();
            count = result.length;
            for(int i=0;i<count;i++){
              coins.add(Coin.fromObject(result[i]));
              debugPrint(coins[i].marketValue.toString());//title
            }
            setState(() {
              this.coins = coins;
              count = count;
            });
            debugPrint("Items "+count.toString());
          }
      );
    });
  }

  String getCurrentAssetsData(coin){

    final dbFuture = helper.initializeDb();
    dbFuture.then((result){
      //final coin = helper.retrieveCoin(coin_name);
      final coinOrderFuture = helper.getCoinOrder(coin);
      double totalCost = 0;
      double totalCount = 0;
      double totalCommission=0;
      coinOrderFuture.then(
              (result){
            List<CoinOrder> coinOrders = List<CoinOrder>();
            int count = result.length;
            for(int i=0;i<count;i++){
              coinOrders.add(CoinOrder.fromObject(result[i]));
              totalCost += coinOrders[i].count * coinOrders[i].buyValue;
              totalCount += coinOrders[i].count;
              totalCommission+= coinOrders[i].commission;
            }

            //averagePrice = totalCost/totalCount;
            //totalCommission = totalCommission;
            //totalCost = totalCost;
            String sData = "Avg: ${totalCost/totalCount} Investment: ${totalCost}";
            debugPrint(sData);
            return sData;

          }
      );
    });
  }

  void navigateToDetail(Coin coin) async{
    bool result = await Navigator.push(context, MaterialPageRoute(builder: (context)=>CoinDetail(coin)),);
    if(result==true){
      getData();
    }
  }

  void navigateToOrders(Coin coin) async{
    bool result = await Navigator.push(context, MaterialPageRoute(builder: (context)=>CoinOrderList(coin)),);
    /*if(result==true){
      getOrderData();
    }*/
  }
}