jsonflutterdartflutter-hive

Unexpected character (at character 2) dart flutter


Getting stored data on my flutter hive makes me fall into this error:

════════ Exception caught by widgets library ═══════════════════════════════════
Unexpected character (at character 2)
{id: 2, network: MTN, discount: 2, logo: mtncard.png}
 ^

Here is my class:

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,
      );
  }
}

and here is my functions:

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();
}

This my future builder:

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,
                    ),
                  ],
                ),
              ],
            ),
          );
        },
      );
    }
}),

How to fix the error?


Solution

  • There is an issue with the JSON decoding, which you can fix by encoding the JSON string first with the jsonEncode function.

    So, your code would look like this:

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

    Refer to the problem and the solution here: https://github.com/flutter/flutter/issues/32841#issuecomment-514454946