'Flutter / Dart: http.get returns no response

I'm trying to retrieve data from two APIs one from my local Laravel application and another website.

The first request is successful but the second returns nothing, not even a status code.

import 'package:http/http.dart' as http;

class StreamAPI {
  Future<Data> getEmbedData() async {
    final videoHorizonResponse = await http.get(
      Uri.http('0.0.0.0', '/api/movie/634649'),
    );

    if (videoHorizonResponse.statusCode != 200) {
      throw Exception('Failed to load title data');
    }

    // prints 200
    print(videoHorizonResponse.statusCode);

    final superEmbedResponse = await http.get(
      Uri.https(
        'seapi.link',
        '/',
        {'type': 'tmdb', 'id': '634649', 'max_results': '1'},
      ),
    );

    // prints nothing
    print(superEmbedResponse.statusCode);

    if (superEmbedResponse.statusCode != 200) {
      throw Exception('Failed to load servers');
    }

    return Data.fromJson(
      jsonDecode(videoHorizonResponse.body)['title_data'],
      jsonDecode(superEmbedResponse.body)['results'],
    );
  }
}

Also when i try retrieving the data in my future builder it returns an error of null.

I'm not sure if i structured the data incorrectly.

class Data {
  final TitleData titleData;
  final StreamsList servers;

  const Data({
    required this.titleData,
    required this.servers,
  });

  factory Data.fromJson(
    Map<String, dynamic> parsedTitleJson,
    List<dynamic> parsedServerJson,
  ) {
    return Data(
      titleData: TitleData.fromJson(parsedTitleJson),
      servers: StreamsList.fromJson(parsedServerJson),
    );
  }
}

class TitleData {
  final String name, type, tagline, description, poster, backdrop;

  const TitleData({
    required this.name,
    required this.type,
    required this.tagline,
    required this.description,
    required this.poster,
    required this.backdrop,
  });

  factory TitleData.fromJson(Map<String, dynamic> json) {
    return TitleData(
      name: json['name'],
      type: json['type'],
      tagline: json['tagline'],
      description: json['description'],
      poster: json['poster'],
      backdrop: json['backdrop'],
    );
  }
}

// ? creates a list of stream objects
class StreamsList {
  final List<Stream> streams;

  StreamsList({
    required this.streams,
  });

  factory StreamsList.fromJson(List<dynamic> parsedJson) {
    List<Stream> streams = <Stream>[];
    streams = parsedJson.map((i) => Stream.fromJson(i)).toList();

    return StreamsList(streams: streams);
  }
}

// ? creates a stream object
class Stream {
  final String server, title, quality, url;

  Stream({
    required this.server,
    required this.title,
    required this.quality,
    required this.url,
  });

  factory Stream.fromJson(Map<String, dynamic> json) {
    return Stream(
      server: json['server'],
      title: json['title'],
      quality: json['quality'],
      url: json['json'],
    );
  }
}

Or if I'm accessing it incorrectly.

class _TitleOverlayState extends State<TitleOverlay> {
  late Future<Data> futureData;

  @override
  void initState() {
    super.initState();
    futureData = StreamAPI().getEmbedData();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Data>(
      future: futureData,
      builder: (context, title) {
        if (title.hasData) {
          return Stack(
            children: [
              Align(
                alignment: Alignment.center,
                child: Builder(builder: (context) {
                  if (MediaQuery.of(context).size.width >
                      MediaQuery.of(context).size.height) {
                    return Image.network(
                      title.data!.titleData.backdrop,
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.height,
                      fit: BoxFit.cover,
                    );
                  } else {
                    return Image.network(
                      title.data!.titleData.poster,
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.height,
                      fit: BoxFit.cover,
                    );
                  }
                }),
              ),

Or perhaps it's because of the null response from the second request.

The entire data set returns a null error.

else if (title.hasError) {
          return Align(
            alignment: Alignment.center,
            child: Material(
              elevation: 5,
              borderRadius: BorderRadius.circular(2.0),
              color: kSecondaryColor,
              child: Container(
                padding: const EdgeInsets.all(20),
                child: Text(
                  // displays null text on the screen
                  '${title.data}',
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.normal,
                    color: Colors.red,
                  ),
                ),
              ),
            ),
          );
        }

I can't seem to wrap my little coder brain around it, please help. Thank you.

When refactoring to test the data from the fist response it works just fine.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source