flutterdart

can't get request in flutter


I was making a simple application for monitoring the prices of cryptocurrencies.

There are only three files in the project.

main.dart

    import 'package:flutter/material.dart';
import 'cc_list.dart';

void main() => runApp(const CCTracker());

class CCTracker extends StatelessWidget {
  const CCTracker({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Awesome CC tracker',
        theme: ThemeData(primarySwatch: 
Colors.green),
        home: const CCList());
  }
}

cc_list.dart

import 'package:crypto_app/cc_data.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:http/http.dart' as http;

class CCList extends StatefulWidget {
  const CCList({super.key});

  @override
  State<StatefulWidget> createState() {
    return CCListState();
  }
}

class CCListState extends State<CCList> {
  List<CCData> data = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Awesome CC Tracker',
          style: GoogleFonts.arsenal(
              fontSize: 35,
              fontStyle: FontStyle.italic,
              color: const Color.fromARGB(255, 
 143, 76, 0),
              fontWeight: FontWeight.w600),
        ),
      ),
      body: ListView(
        children: _buildList(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _loadCC(),
        child: const Icon(Icons.refresh),
      ),
    );
  }

  _loadCC() async {
var response = await http.get(
  headers: {
    'X-CMC_PRO_API_KEY': 'a6343852-13ad-4665-8d01- 
2b7141d54544',
  },
  Uri.parse('https://pro- 
api.coinmarketcap.com/v1/cryptocurrency/map'),
);
if (response.statusCode == 200) {
  debugPrint(response.body);
}

}

  List<ListTile> _buildList() {
    return data
        .map((CCData f) => ListTile(
              subtitle: Text(f.symbol),
              title: Text(f.name),
              leading: CircleAvatar(child: 

Text(f.rang.toString())),
              trailing: 
Text('\$${f.price.toString()}'),
            ))
        .toList();
  }
}

cc_data.dart

    class CCData {
  String name;
  String symbol;
  int rang;
  double price;

  CCData(
      {  required this.name,
      required this.price,
      required this.rang,
      required this.symbol});
}

Dart doesn't throw any errorsenter image description here

when i click on the button, the program should receive and display data from the api to the console, but this does not happen. Please help me understand what's going on here.

I fixed the code and added the api key to the request, but it did not help. app looks the same

enter image description here

got a new error

CRITICAL **: 15:13:17.703: Failed to read XDG desktop portal settings: GDBus.Error:org.freedesktop.portal.Error.NotFound: Requested setting not found ** (crypto_app:139699): CRITICAL **: 15:13:17.715: Failed to read XDG desktop portal settings: GDBus.Error:org.freedesktop.portal.Error.NotFound: Requested setting not found Connecting to VM Service at ws://127.0.0.1:33101/LQci33Mhje0=/ws


Solution

  • Response for your request returns a non 200 response.

    You should handle this and print/display appropriate result. Currently this api returns a 401 since your code does not have an API specified.

    Check coinmarketcap API docs to see how to do this.

    Also you can put a breakpoint at the if condition where you're checking the statusCode and check the response in your editor.