skip to Main Content
// ignore_for_file: avoid_print

import 'dart:convert';

import 'package:ftmovie/src/data/core/api_constants.dart';
import 'package:ftmovie/src/data/models/movie_model.dart';
import 'package:ftmovie/src/data/models/movies_result_model.dart';
import 'package:http/http.dart';

abstract class MovieRemoteDataSource {
  Future<List<MovieModel>> getTrending();
}

class MovieRemoteDataSourceImpl extends MovieRemoteDataSource {
  final Client _client;

  MovieRemoteDataSourceImpl(this._client);

  @override
  Future<List<MovieModel>> getTrending() async {
    final response = await _client.get(
      '${ApiConstants.BASE_URL}trending/movie/day?api_key=${ApiConstants.API_KEY}',
      headers: {
        'Content-Type': 'application/json',
      },
    );

    if (response.statusCode == 200){
      final responseBody = json.decode(response.body);
      final movies = MoviesResultModel.fromJson(responseBody).movies;
      print(movies);
      return movies;
    } else {
      throw Exception(response.reasonPhrase);
    }
  }
}

The part where I get the error: ‘${ApiConstants.BASE_URL}trending/movie/day?api_key=${ApiConstants.API_KEY}’, where the code is. I would appreciate your help.

I also asked my friends who know a little flutter. I tried everything they wrote on the internet, but I can’t print and see the data in any way.

2

Answers


  1. get takes an Uri object, not a String. Fortunately it’s very easy to convert. Just write

    final response = await _client.get(
      Uri.parse('${ApiConstants.BASE_URL}trending/movie/day?api_key=${ApiConstants.API_KEY}'),
      headers: {
        'Content-Type': 'application/json',
      },
    );
    
    Login or Signup to reply.
  2. you can use static method of Uri class for parsing String to Uri like below:

    final uri = Uri.parse("your_string");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search