dart 非预期字符(字符2处)省道抖动

icomxhvb  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(61)

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

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

意外字符(位于字符2处){id:2、网络:MTN,折扣:2、logo: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,
                                                            ),
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  );
                                                },
                                              );
                                            }
                                          }),

我新扑开发我不知道问题在哪里,但会很高兴得到一个解决方案,感谢您在我的问题的影响

tjrkku2a

tjrkku2a1#

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

相关问题