意外字符(在字符 2 处)飞镖颤动

问题描述 投票:0回答:1

在我的 flutter hive 上获取存储的数据让我陷入了这个错误:

════════ Exception caught by widgets library ═══════════════════════════════════

意外字符(字符 2) {id:2,网络:MTN,折扣:2,徽标:mtncard.png} ^

这是我的课:

class Provider {
Provider({
required this.id,
required this.network,
required this.discount,
required this.logo,
});
final String id;
final String network;
final int discount;
final String logo;

factory Provider.fromJson(Map<String, dynamic> data) {
final id = data['id'] as String;
final network = data['network'] as String;
final discount = int.parse(data['discount']);
final logo = data['logo'] as String;
return Provider(
  id: id,
  network: network,
  discount: discount,
  logo: logo,
  );
 }
}

这是我的功能:

Future<List<dynamic>> LoadData() async {
final name = await Hive.box<dynamic>('my_db');
final result = name.values.toList();
print(result);
//print result is gotten perfectly from hive box
return name.values.toList();
}

这也是我的未来建设者

  FutureBuilder<List<dynamic>>(
                                          future: LoadData(),
                                          builder: (BuildContext context,
                                              AsyncSnapshot snapshot) {
                                            if (snapshot.data == null) {
                                              return const Center(
                                                child: Text('loading'),
                                              );
                                            } else {
                                              return ListView.builder(
                                                itemCount:
                                                    snapshot.data.length,
                                                itemBuilder:
                                                    (BuildContext context,
                                                        int index) {
                                                  // Decoding the string to Map
                                                  final Map<String, dynamic>
                                                      decodedData =
                                                      jsonDecode(snapshot
                                                          .data[index]);

                                                  // Mapping the Map to Provider object
                                                  final itemData =
                                                      Provider.fromJson(
                                                          decodedData);

                                                  return ListTile(
                                                    title: Row(
                                                      crossAxisAlignment:
                                                          CrossAxisAlignment
                                                              .center,
                                                      children: [
                                                        SizedBox(
                                                          width: 20,
                                                        ),
                                                        Row(
                                                          mainAxisAlignment:
                                                              MainAxisAlignment
                                                                  .spaceEvenly,
                                                          children: [
                                                            Text(
                                                              itemData
                                                                .discount
                                                                    .toString(),
                                                              style:
                                                                  const TextStyle(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .bold,
                                                                fontSize:
                                                                    20,
                                                                color: Colors
                                                                    .green,
                                                              ),
                                                            ),
                                                            Icon(
                                                              Icons
                                                                  .more_horiz,
                                                              color: Colors
                                                                  .blue,
                                                            ),
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  );
                                                },
                                              );
                                            }
                                          }),

我是新的 flutter 开发人员,我不知道问题出在哪里,但很高兴能得到解决方案,感谢您对我的问题的影响

json flutter dart hive
1个回答
0
投票

JSON 解码存在问题,您可以通过首先使用

jsonEncode
函数对 JSON 字符串进行编码来修复该问题,因此您的代码将如下所示:

// Decoding the string to Map
final Map<String, dynamic> decodedData =
  jsonDecode(jsonEncode(snapshot.data[index]));

请参阅此处的问题和解决方案:https://github.com/flutter/flutter/issues/32841#issuecomment-514454946

© www.soinside.com 2019 - 2024. All rights reserved.