【问题标题】:type 'string' is not a subtype of type 'int' of 'index' in flutter firebase database类型'string'不是flutter firebase数据库中'index'类型'int'的子类型
【发布时间】:2021-12-15 11:09:01
【问题描述】:

当我运行这段代码时,我得到一个错误: “String”类型不是“index”类型“int”的子类型,应用程序无法运行。有人可以告诉我问题出在哪里或为什么会出现此错误吗? 我在这里尝试从 firebase 实时数据库中检索数据。

void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
    
      FirebaseMessaging.onBackgroundMessage(backgroundHandler);
      runApp(NGU_APP());
    }
    
    // ignore: camel_case_types
    class NGU_APP extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyHomepage(),
          routes: {
            "login": (_) => LoginScreen(),
            "signup": (_) => RegistrationScreen(),
          },
        );
      }
    }
    
    class MyHomepage extends StatefulWidget {
      const MyHomepage({key}) : super(key: key);
      @override
      _MyHomepageState createState() => _MyHomepageState();
    }
    
    class _MyHomepageState extends State<MyHomepage> {
     @override
      void initState() {
        super.initState();
        // getmessages();
        dbref = FirebaseDatabase.instance.reference();
    
    @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            backgroundColor: Color(0xFF6D0131),
            body: SafeArea(
                child: FirebaseAnimatedList(
              shrinkWrap: true,
              query: dbref,
              itemBuilder: (BuildContext context, DataSnapshot snapshot,
                  Animation animation, int index) {
                // var x = snapshot.value['Login'];
                // print(x);
                return ListTile(
                  title: Text(snapshot.value['Main Page']),
                );
              },
            )),
          ),
        );
    }
    }

【问题讨论】:

    标签: flutter firebase-realtime-database


    【解决方案1】:

    快照是一个 JSON 数组。尝试使用:

    snapshot.value[index]['Main Page'] 
    

    代替:

    snapshot.value['Main Page']
    

    【讨论】:

    • 我试过了,但是没用。
    • 所以,请试试这个 snapshot.value.toString()
    • 另外,尝试检查快照 snapshot.value.forEach((k,v) { print(k); print(v); });
    • 很遗憾,也没有用。
    • 能否提供打印快照的结果?
    猜你喜欢
    • 2020-04-22
    • 2020-10-02
    • 2021-05-10
    • 2019-04-24
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2022-11-25
    相关资源
    最近更新 更多