2

I am having a problem woth my code when connecting API's in flutter does anyone know how I can solve this error?

import 'package:ctrade/constants/strings.dart';
import 'package:ctrade/models/Users.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class Services {

  static const String url = 'https://linkhere.co.zw';

  static Future<List<Markets>> getMarkets() async{
    try{
      final response = await http.get(Uri.parse(url));
      if(200 == response.statusCode) {
        final List<Markets> markets = marketsFromJson(response.body);
        return markets;
      }else {
        return List<Markets>(); //error here
      }

    }catch(e){
      return List<Markets>(); //error here
    }
  }
}
Panashe
  • 99
  • 1
  • 10

1 Answers1

2

Use a literal instead:

return [];

The compiler will use type inference to construct a list of the proper type.


Side note: Generally, Dart/Flutter is pretty helpful with error messages - The solution to your problem is in the message:

The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'.

Michael Horn
  • 3,819
  • 9
  • 22