Home > Mobile >  Unhandled Exception: FormatException: Unexpected character (at character 1) | Error
Unhandled Exception: FormatException: Unexpected character (at character 1) | Error

Time:11-08

I have a problem when logging in, it does not show me the data in the database, it seems that the problem is in the function that fetches the data, but I don't know where the problem is

This is the function that fetches data from the database:

Future<void> fetchProperties() async {
  final url = Uri.https(
      'aqarlibya-4d39c-default-rtdb.europe-west1.firebasedatabase.app',
      '/properties.json?auth=$authToken');
  try {
    final response = await http.get(url);
    final extractedData = json.decode(response.body) as Map<String, dynamic>?;
    if (extractedData == null) {
      return;
    }
    final List<Property> loadedProperties = [];
    extractedData.forEach((propId, propData) {
      loadedProperties.add(Property(
        id: propId,
        name: propData['name'],
        description: propData['description'],
        type: propData['type'],
        propertySize: propData['propertySize'],
        bedrooms: propData['bedrooms'],
        price: propData['price'],
        cityId: propData['cityId'],
        imageUrl: propData['imageUrl'],
        isFav: propData['isFav'],
      ));
    });
    _items = loadedProperties;
    notifyListeners();
  } catch (error) {
    throw (error);
  }
}

This is part of the code in the main file:

Widget build(BuildContext context) {
return MultiProvider(
  providers: [
    ChangeNotifierProvider.value(
      value: Auth(),
    ),
    ChangeNotifierProxyProvider<Auth, Properties>(
      create: (ctx) => Properties('', []),
      update: (ctx, auth, previousProperties) => Properties(
        auth.token,
        previousProperties == null ? [] : previousProperties.items,
      ),
    ),
  ],

I tried looking for the problem but couldn't find it

enter image description here

CodePudding user response:

As far as I can see from the documentation of Uri.https, you need to pass parameters in the optional third argument. So:

final url = Uri.https(
   'aqarlibya-4d39c-default-rtdb.europe-west1.firebasedatabase.app',
   '/properties.json',
   { 'auth': authToken }
);

CodePudding user response:

Are you sure your url and your token is good ?

It seems like you try to json decode something that is not a json. The value return by your call seems to be "not found".

Try to add a try {} catch(e) {} and print responseto get a better view of the error.

  • Related