0

I have an error when getting data from an api. I'm using get connect for this but I'm getting an error.

my api has a bearer token, and i'm already getting this token on startup using shared preferences, but when i start the app i get an error that i had previously placed a snackbar to handle this error

this is my json

{
    "avatar_url": "https://sollerassessoria-homolog.s3.sa-east-1.amazonaws.com/pessoas/21/avatar.png",
    "email": "influenciador@teste.com.br",
    "influenciador_id": 21,
    "nome": "isabela",
    "nome_artistico": "Influenciador Teste",
    "responsavel": {
        "avatar_url": "https://sollerassessoria-homolog.s3.sa-east-1.amazonaws.com/pessoas/3/avatar.png",
        "email": "financeiro@sollerassessoria.com.br",
        "nome": "Sonny",
        "sobrenome": "Lima",
        "staff_id": 3
    },
    "sobrenome": "lourenco soller"
}

this is my model

class CurrentInfluencerModel {
  
  final String avatarUrl;
  final String email;
  final int influenciadorId;
  final String nome;
  final String nomeArtistico;
  final String sobrenome;
  
  CurrentInfluencerModel({
    required this.avatarUrl,
    required this.email,
    required this.influenciadorId,
    required this.nome,
    required this.nomeArtistico,
    required this.sobrenome,
  });


  
  

  Map<String, dynamic> toMap() {
    return {
      'avatar_url': avatarUrl,
      'email': email,
      'influenciador_id': influenciadorId,
      'nome': nome,
      'nome_artistic': nomeArtistico,
      'sobrenome': sobrenome,
    };
  }

  factory CurrentInfluencerModel.fromMap(Map<String, dynamic> map) {
    return CurrentInfluencerModel(
      avatarUrl: map['avatar_url'],
      email: map['email'],
      influenciadorId: map['influenciador_id'],
      nome: map['nome'],
      nomeArtistico: map['nome_artistico'],
      sobrenome: map['sobrenome'],
    );
  }

  String toJson() => json.encode(toMap());

  factory CurrentInfluencerModel.fromJson(String source) => CurrentInfluencerModel.fromMap(json.decode(source));
}

this is my method

class InfluencerRepositoryImpl implements InfluencerRepository {
  final RestClient _restClient;

  InfluencerRepositoryImpl({required RestClient restClient})
      : _restClient = restClient;

  @override
  Future<List<CurrentInfluencerModel>> getPrimaryInfos() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

    final String token = sharedPreferences.getString("token") ?? "";

    final Response result = await _restClient
        .get<List<CurrentInfluencerModel>>("/auth/user", headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token'
    }, decoder: (data) {
      final results = data;
      if (results != null) {
        return results
            .map<CurrentInfluencerModel>(
                (i) => CurrentInfluencerModel.fromMap(i))
            .toList();
      }
      return <CurrentInfluencerModel>[];
    });

    if (result.hasError) {
      print("Status ${result.statusCode}");
      throw ("erro ao buscar dados");
    }

    return result.body ?? <CurrentInfluencerModel>[];
  }
}

and this is my error, apparently it's coming when i call the map

I/flutter ( 9977): NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'map' with matching arguments.
I/flutter ( 9977): Receiver: _LinkedHashMap len:7
I/flutter ( 9977): Tried calling: map<CurrentInfluencerModel>(Closure: (dynamic) => CurrentInfluencerModel)
I/flutter ( 9977): Found: map<Y0, Y1>((X0, X1) => MapEntry<Y0, Y1>) => Map<Y0, Y1>
I/flutter ( 9977): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:63:5)
I/flutter ( 9977): #1      InfluencerRepositoryImpl.getPrimaryInfos.<anonymous closure>
package:soller_acessoria_new_app_nullsafety/…/influencer/influencer_repository_impl.dart:29
I/flutter ( 9977): #2      bodyDecoded
package:get/…/utils/body_decoder.dart:28
I/flutter ( 9977): #3      HttpRequestImpl.send
package:get/…/io/http_request_io.dart:60
I/flutter ( 9977): <asynchronous suspension>
I/flutter ( 9977): #4      GetHttpClient._performRequest
package:get/…/src/http.dart:209
I/flutter ( 9977): <asynchronous suspension>
I/flutter ( 9977): #5      GetHttpClient.get
package:get/…/src/http.dart:455
I/flutter ( 9977): <asynchronous suspension>
I/flutter ( 9977): #6      InfluencerRepositoryImpl.getPrimaryInfos
package:soller_acessoria_new_app_nullsafety/…/influencer/influencer_repository_impl.dart:20
I/flutter ( 9977): <asynchronous suspension>
I/flutter ( 9977): #7      HomeController.onReady
package:soller_acessoria_new_app_nullsafety/…/home/home_controller.dart:47
I/flutter ( 9977): <asynchronous suspension>

this is my view page,the last item is where I call my model to display the data on the screen:

class HomePage extends GetView<HomeController> {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            children: [
              const Padding(
                padding: EdgeInsets.only(top: 88),
                child: CircleAvatar(
                  backgroundColor: Colors.black,
                  maxRadius: 107,
                  child: CircleAvatar(
                    backgroundColor: Colors.grey,
                    backgroundImage: NetworkImage(
                      //!IMAGEM DO INFLUENCER QUE VEM DA API
                      "https://www.petz.com.br/blog/wp-content/uploads/2021/03/piercing-para-cachorro-2.jpg",
                    ),
                    maxRadius: 100,
                  ),
                ),
              ),
              Padding(
                  padding: const EdgeInsets.only(top: 16),
                  child: Obx(() {
                    return ListView(
                      shrinkWrap: true,
                      children: controller.influencerDataList
                          .map((data) => Text(data.nome))
                          .toList(),
                    );
                  })),
              Row(
  • I believe this might be what is causing a problem: `return results.map(` Because results may or may not be a map, instead of a list, would you mind showing what fultter outputs if you print results? – h8moss Oct 25 '21 at 20:50
  • tanks my friend i solve the problem, is the same problem do you related – flavio_vieira_stack Oct 28 '21 at 14:33

0 Answers0