SetState Flutter导致错误:类型'int'不是类型'String'的子类型

时间:2020-05-24 04:56:40

标签: flutter dart google-cloud-firestore setstate flutter-provider

我正在使用Flutter制作一个应用程序,我想在其中显示用户的购买并有一个按钮来隐藏和显示有关购买的图表。整个应用程序都连接到Firebase Cloud Firestore后端,因此我使用Provider来处理状态管理。每当在主页(UserPage)上调用任何变量的setState,而没有将其链接到其他变量时,我都会收到错误消息:

类型'int'不是类型'String'的子类型

相关的引起错误的小部件是

购买清单

我尝试只运行setState函数而没有任何代码,就像这样:

onPressed: () {setState(() {});}

,仍然出现相同的错误。

main.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'databaseservice.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: StreamProvider<List<User>>.value(
          initialData: [],
          value: DatabaseService('AuthenticaionUID').accountUsers,
          lazy: false,
          child: UserPage(1)),
    );
  }
}

class UserPage extends StatefulWidget {
  final int userID;
  UserPage(this.userID);
  @override
  _UserPageState createState() => _UserPageState(userID);
}

class _UserPageState extends State<UserPage> {
  final int userID;
  _UserPageState(this.userID);

  // String error = '';
  bool showChart = true;

  @override
  Widget build(BuildContext context) {
    var usersList = Provider.of<List<User>>(context);
    var user;
    usersList.forEach((User userI) {
      if (userI.id == userID) {
        user = userI;
      }
    });

    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.red,
        title: Text('${user.name}\'s Budget'),
      ),
      body: Padding(
          padding: EdgeInsets.fromLTRB(8, 8, 8, 0),
          // padding: EdgeInsets.all(8),
          child: (user.total == 0)
              ? Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Expanded(
                      child: Center(child: Text('No purchases')),
                    )
                  ],
                )
              : Column(
                  children: <Widget>[
                    showChart
                        ? Container(
                            child: UserChart(user),
                            height: 269,
                          )
                        : Container(),
                    PurchaseList(userID),
                  ],
                )),
      bottomNavigationBar: BottomAppBar(
        child: IconButton(
            icon: Icon(Icons.insert_chart),
            onPressed: () {
              setState(() {
                showChart = !showChart;
              });
            }),
      ),
    );
  }
}

class PurchaseList extends StatelessWidget {
  final int userID;
  PurchaseList(this.userID);
  @override
  Widget build(BuildContext context) {
    List<User> usersList = Provider.of<List<User>>(context);
    User user;
    usersList.forEach((User userI) {
      if (userI.id == userID) {
        user = userI;
      }
    });
    return Expanded(
      child: Container(
        child: ListView.builder(
          itemCount: user.purchases.length,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: Row(
                children: <Widget>[
                  Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(user.purchases[index]['name']),
                        Text(user.purchases[index]['category']),
                        Text(user.purchases[index]['date']),
                        Text(user.purchases[index]['shop'])
                      ]),
                  // Price
                  Text('\$${user.purchases[index]["price"]}'),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}

非常感谢您的帮助,谢谢

3 个答案:

答案 0 :(得分:1)

再次在Firebase控制台中检查您的数据类型。例如,您的userID可能是一个与userI.id比较的字符串。添加.toString()应该可以解决问题。

答案 1 :(得分:0)

所以错误是因为期望的参数是int值并且正在获取String,请检查参数类型或将int转换为String

答案 2 :(得分:0)

就这样做,我从firestore中检索了我的手机号码,它是int值

Text(document["mobileNo"].toString())