dev/flutter

[Flutter] Http API통신

캄춰 2023. 12. 27. 15:25
728x90
반응형
dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  http: ^1.1.2

 

 

import 

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

 

 

함수

void _request() async {
  try {
    final url = Uri.parse('https://example.com/test);
    http.Response response = await http.get(url);

    print('_request.. response:$response');
    print('_request.. response:${response.body}');

    if (response.statusCode == 200) {
      final data =jsonDecode(response.body);	// dynamic type
      print('data... $data');
    }
  } catch (e) {
    throw Exception('_request error');
  }
}

 

 

 

728x90
반응형