Home > Mobile >  Flutter Http get coming Null
Flutter Http get coming Null

Time:02-11

I started to learn work with APIs. I tried an api without authentication reqres.in and it worked for me . But now im trying to get datas from a api which is work with key, and this datas coming null. When i used my key like this on web browsers https://api.rawg.io/api/games?key=MY_API_KEY&page=1 its working ( i can see JSON data). But when i use this api key for my working code datas coming null and i couldn't understand why.

Here is my code

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  Future<Welcome> apiCall() async {
    final response = await http.get(Uri.parse(
        'https://api.rawg.io/api/games?key=84e37baf...(mykey)&page=1'));

    return Welcome.fromJson(jsonDecode(response.body));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder<Welcome>(
          future: apiCall(),
          builder: (context, snapshot) {
            var growableList = [];
            List<Result> data = snapshot.data!.results;
            growableList.add(data[0].name);
            return Text('${growableList}');
          },
        ),
      ),
    );
  }
}

CodePudding user response:

FutureBuilders work by calling the async function and then immediately calling build. This is why you're seeing null. They will rebuild again once the future returns.

Even though you've marked apiCall as async the builder will not wait for it to finish. What you need to do is check snapshot.hasData which will only be true once the future has completed. When snapshot.hasData is false, you need to return a different widget.

It might also be worth using snapshot.hasError which is a bool representing if an error occurred while completing the async function.

Try this code out

import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  Future<Welcome> apiCall() async {
    final response = await http.get(Uri.parse(
        'https://api.rawg.io/api/games?key=84e37baf...(mykey)&page=1'));

    return Welcome.fromJson(jsonDecode(response.body));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder<Welcome>(
          future: apiCall(),
          builder: (context, snapshot) {
            if(snapshot.hasError){
              print(snapshot.error);
            }
            if(snapshot.hasData){
              var growableList = [];
              List<Result> data = snapshot.data!.results;
              growableList.add(data[0].name);
              return Text('${growableList}');
            } else {
              return CircularProgressIndicator();
            }
          },
        ),
      ),
    );
  }
}

CodePudding user response:

I fixed the problem. Problem was in dataclass, there was some classes which has same names and variable problems like int or double, For exmaple When i tried to generate my code with looking example response i used int except double. But response in real getting double variables. Mistakes like this doesn't let me print it.

  • Related