Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Flutter Error and Debugging tips

Error1

type 'Null' is not a subtype of type 'String'
Enter fullscreen mode Exit fullscreen mode

my existing code is

import 'dart:convert';


class SearchInfluencer {
int id;
String userId;
String userName;
String userEmail;
String filePic;
String slugId;
String slug;
String slugname;
String countryId;
String stateId;
String cityId;
String mobile;
String digitalMarketer;
String bio;
String socialSite;
String socialPrice;
String socialCurrency;
String adminId;
String cartId;
String influencerAdminId;
String cartSocials;
String currency;
String influencerEmail;

SearchInfluencer({
this.id = 0,
required this.userId,
required this.userName,
required this.userEmail,
required this.filePic,
required this.slugId,
required this.slug,
required this.slugname,
required this.countryId,
required this.stateId,
required this.cityId,
required this.mobile,
required this.digitalMarketer,
required this.bio,
required this.socialSite,
required this.socialPrice,
required this.socialCurrency,
required this.adminId,
required this.cartId,
required this.influencerAdminId,
required this.cartSocials,
required this.currency,
required this.influencerEmail,
});

factory SearchInfluencer.fromJson(Map<String, dynamic> map) {
return SearchInfluencer(
id: map["id"],
userId: map["user_id"],
userName: map["user_name"],
userEmail: map["user_email"],
filePic: map["file_pic"],
slugId: map["slug_id"],
slug: map["slug"],
slugname: map["slugname"],
countryId: map["country_id"],
stateId: map["state_id"],
cityId: map["city_id"],
mobile: map["mobile"],
digitalMarketer: map["digital_marketer"],
bio: map["bio"],
socialSite: map["social_site"],
socialPrice: map["social_price"],
socialCurrency: map["social_currency"],
adminId: map["admin_id"],
cartId: map["cart_id"],
influencerAdminId: map["influencer_admin_id"],
cartSocials: map["cart_socials"],
currency: map["currency"],
influencerEmail: map["influencer_email"],
);
}

Map<String, dynamic> toJson() {
return {
"id": id,
"user_id": userId,
"user_name": userName,
"user_email": userEmail,
"file_pic": filePic,
"slug_id": slugId,
"slug": slug,
"slugname": slugname,
"country_id": countryId,
"state_id": stateId,
"city_id": cityId,
"mobile": mobile,
"digital_marketer": digitalMarketer,
"bio": bio,
"social_site": socialSite,
"social_price": socialPrice,
"social_currency": socialCurrency,
"admin_id": adminId,
"cart_id": cartId,
"influencer_admin_id": influencerAdminId,
"cart_socials": cartSocials,
"currency": currency,
"influencer_email": influencerEmail,
};
}

@override
String toString() {
return 'SearchInfluencer{id: $id, userId: $userId, userName: $userName, userEmail: $userEmail, filePic: $filePic, slugId: $slugId, slug: $slug, slugname: $slugname, countryId: $countryId, stateId: $stateId, cityId: $cityId, mobile: $mobile, digitalMarketer: $digitalMarketer, bio: $bio, socialSite: $socialSite, socialPrice: $socialPrice, socialCurrency: $socialCurrency, adminId: $adminId, cartId: $cartId, influencerAdminId: $influencerAdminId, cartSocials: $cartSocials, currency: $currency, influencerEmail: $influencerEmail}';
}

bool startsWith(String query) {
// return userName.toLowerCase().contains(query.toLowerCase());
return userName?.toLowerCase().contains(query.toLowerCase()) ?? false;

}
}

List<SearchInfluencer> searchInfluencerFromJson(String jsonData) {
final data = json.decode(jsonData);
return List<SearchInfluencer>.from(data.map((item) => SearchInfluencer.fromJson(item)));
}

String searchInfluencerToJson(SearchInfluencer data) {
final jsonData = data.toJson();
return json.encode(jsonData);
} 
Enter fullscreen mode Exit fullscreen mode

Solution

 String? userId;
Enter fullscreen mode Exit fullscreen mode
  bool startsWith(String query) {
    return userName?.toLowerCase().contains(query.toLowerCase()) ?? false;
  }
Enter fullscreen mode Exit fullscreen mode
import 'dart:convert';

class SearchInfluencer {
  int id;
  String? userId;
  String? userName;
  String? userEmail;
  String? filePic;
  String? slugId;
  String? slug;
  String? slugname;
  String? countryId;
  String? stateId;
  String? cityId;
  String? mobile;
  String? digitalMarketer;
  String? bio;
  String? socialSite;
  String? socialPrice;
  String? socialCurrency;
  String? adminId;
  String? cartId;
  String? influencerAdminId;
  String? cartSocials;
  String? currency;
  String? influencerEmail;

  SearchInfluencer({
    this.id = 0,
    this.userId,
    this.userName,
    this.userEmail,
    this.filePic,
    this.slugId,
    this.slug,
    this.slugname,
    this.countryId,
    this.stateId,
    this.cityId,
    this.mobile,
    this.digitalMarketer,
    this.bio,
    this.socialSite,
    this.socialPrice,
    this.socialCurrency,
    this.adminId,
    this.cartId,
    this.influencerAdminId,
    this.cartSocials,
    this.currency,
    this.influencerEmail,
  });

  factory SearchInfluencer.fromJson(Map<String, dynamic> map) {
    return SearchInfluencer(
      id: map["id"] ?? 0,
      userId: map["user_id"],
      userName: map["user_name"],
      userEmail: map["user_email"],
      filePic: map["file_pic"],
      slugId: map["slug_id"],
      slug: map["slug"],
      slugname: map["slugname"],
      countryId: map["country_id"],
      stateId: map["state_id"],
      cityId: map["city_id"],
      mobile: map["mobile"],
      digitalMarketer: map["digital_marketer"],
      bio: map["bio"],
      socialSite: map["social_site"],
      socialPrice: map["social_price"],
      socialCurrency: map["social_currency"],
      adminId: map["admin_id"],
      cartId: map["cart_id"],
      influencerAdminId: map["influencer_admin_id"],
      cartSocials: map["cart_socials"],
      currency: map["currency"],
      influencerEmail: map["influencer_email"],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      "id": id,
      "user_id": userId,
      "user_name": userName,
      "user_email": userEmail,
      "file_pic": filePic,
      "slug_id": slugId,
      "slug": slug,
      "slugname": slugname,
      "country_id": countryId,
      "state_id": stateId,
      "city_id": cityId,
      "mobile": mobile,
      "digital_marketer": digitalMarketer,
      "bio": bio,
      "social_site": socialSite,
      "social_price": socialPrice,
      "social_currency": socialCurrency,
      "admin_id": adminId,
      "cart_id": cartId,
      "influencer_admin_id": influencerAdminId,
      "cart_socials": cartSocials,
      "currency": currency,
      "influencer_email": influencerEmail,
    };
  }

  @override
  String toString() {
    return 'SearchInfluencer{id: $id, userId: $userId, userName: $userName, userEmail: $userEmail, filePic: $filePic, slugId: $slugId, slug: $slug, slugname: $slugname, countryId: $countryId, stateId: $stateId, cityId: $cityId, mobile: $mobile, digitalMarketer: $digitalMarketer, bio: $bio, socialSite: $socialSite, socialPrice: $socialPrice, socialCurrency: $socialCurrency, adminId: $adminId, cartId: $cartId, influencerAdminId: $influencerAdminId, cartSocials: $cartSocials, currency: $currency, influencerEmail: $influencerEmail}';
  }

  bool startsWith(String query) {
    return userName?.toLowerCase().contains(query.toLowerCase()) ?? false;
  }
}

List<SearchInfluencer> searchInfluencerFromJson(String jsonData) {
  final data = json.decode(jsonData);
  return List<SearchInfluencer>.from(data.map((item) => SearchInfluencer.fromJson(item)));
}

String searchInfluencerToJson(SearchInfluencer data) {
  final jsonData = data.toJson();
  return json.encode(jsonData);
}
Enter fullscreen mode Exit fullscreen mode

Error2:The argument type 'String' can't be assigned to the parameter type 'int'.

final selectedCity = countryViewModel.cities.firstWhere(
                          (city) => city.city_name == newValue,
                          orElse: () => City(city_id: 'All', city_name: 'All', state_id: 0, country_id: 'All'),
                        );
Enter fullscreen mode Exit fullscreen mode

Solution

city_id: 0
Enter fullscreen mode Exit fullscreen mode
 setState(() {
                        final selectedCity = countryViewModel.cities.firstWhere(
                          (city) => city.city_name == newValue,
                          orElse: () => City(city_id: 0, city_name: 'All', state_id: 0, country_id: 'All'),
                        );
Enter fullscreen mode Exit fullscreen mode

Error3:

 type 'Null' is not a subtype of type 'int'
Enter fullscreen mode Exit fullscreen mode
  factory States.fromJson(Map<String, dynamic> map) {
    return States(
      id: map["id"] ?? 0,
      state_id: map["state_id"] ?? 0,
      state_name: map["state_name"] ?? '',
      country_id: map["country_id"] ?? '',
    );
  }
Enter fullscreen mode Exit fullscreen mode

full code solution

import 'dart:convert';

class States {
  int id;
  int state_id;
  String state_name;
  String country_id;

  States({
    this.id = 0,
    required this.state_id,
    required this.state_name,
    required this.country_id,
  });

  factory States.fromJson(Map<String, dynamic> map) {
    return States(
      id: map["id"] ?? 0,
      state_id: map["state_id"] ?? 0,
      state_name: map["state_name"] ?? '',
      country_id: map["country_id"] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      "id": id,
      "state_id": state_id,
      "state_name": state_name,
      "country_id": country_id,
    };
  }

  @override
  String toString() {
    return 'States{id: $id, state_id: $state_id, state_name: $state_name, country_id: $country_id}';
  }
}

List<States> stateFromJson(String jsonData) {
  final data = json.decode(jsonData);
  return List<States>.from(data.map((item) => States.fromJson(item)));
}

String stateToJson(States data) {
  final jsonData = data.toJson();
  return json.encode(jsonData);
}
Enter fullscreen mode Exit fullscreen mode

error4:

type 'int' is not a subtype of type 'String'
Enter fullscreen mode Exit fullscreen mode
country_id: map["country_id"].toString(), // Convert to String
Enter fullscreen mode Exit fullscreen mode
 factory States.fromJson(Map<String, dynamic> map) {
    return States(
      id: map["id"] ?? 0,
      state_id: map["state_id"] ?? 0,
      state_name: map["state_name"] ?? '',
      country_id: map["country_id"].toString(), // Convert to String
    );
  }
Enter fullscreen mode Exit fullscreen mode

Debug

Debugging while calling api

void fetchInfluencers() {
  // Fetch data from an API or a local source
  // For example purposes, let's use a static list
  final response = await fetchInfluencersFromApi();
  print(response); // Log the response to see the raw data
  influencers = response.map((data) => SearchInfluencer.fromJson(data)).toList();
}
Enter fullscreen mode Exit fullscreen mode
 countries = await api.getcountry();
      print("Fetched countries:");
       print("Fetched countries: ${countries.map((c) => c.country_name).toList()}");
Enter fullscreen mode Exit fullscreen mode

Debugging while calling provider

  final influencerViewModel = Provider.of<InfluencerViewModel>(context);
    final countryViewModel = Provider.of<CountryViewModel>(context);
    final tagViewModel = Provider.of<TagViewModel>(context);
   print("Selected country: $_selectedCountry");
    print("Available countries: ${countryViewModel.countries.map((c) => c.country_name).toList()}");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)