Home > OS >  call parameter function to get data in flutter
call parameter function to get data in flutter

Time:11-24

I'm learning and trying to add parameters when calling parameters in functions when getting data from the API, but I'm a bit confused about how I call them in widgets.

static Future<Map<String, DataKuliahModel>> getDataKuliah(String smt) async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    await Future.delayed(const Duration(milliseconds: 1000));
    // String responseJson = await rootBundle.loadString('assets/1.json');

    Map<String, DataKuliahModel> finalResult = {};
    final response = await http.get(
      Uri.parse(
        '$url/auth/mhs_siakad/perwalian/get_paket',
      ),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );
    final result = jsonDecode(response.body)['data'] as Map<String, dynamic>;
    result.forEach((key, value) {
      DataKuliahModel dataKuliah = DataKuliahModel.fromMap(value);
      finalResult.addAll({
        key: dataKuliah,
      });
    });
    return finalResult;
  }

and I want to call him here enter image description here

CodePudding user response:

When you declare a function with positional parameters you need to provide those parameters when you call that function.

import 'package:flutter/material.dart';

class Services {
  static Future<String> greeting(String name) async {
    /// this function doesn't need to be Future
    /// but when you call API to get some data it should be a Future

    return 'Hello $name';
  }
}

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

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(

      /// pass positional parameter to [greeting] here
      future: Services.greeting('Dash'), 
      builder: (context, AsyncSnapshot<String> snapshot) {
        return Center(
          child: Text(snapshot.data ?? 'default'),
        );
      },
    );
  }
}

Result: Hello Dash

In your case smt seems to be an int not a String and you have to pass it as query parameter to http request as follows

static Future<Map<String, DataKuliahModel>> getDataKuliah(int smt) async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    await Future.delayed(const Duration(milliseconds: 1000));
    // String responseJson = await rootBundle.loadString('assets/1.json');

    Map<String, DataKuliahModel> finalResult = {};
    final response = await http.get(
     // Uri.parse(
      //  '$url/auth/mhs_siakad/perwalian/get_paket',
     // ),
        Uri.http(url, '/auth/mhs_siakad/perwalian/get_paket', 
        {'smt':smt}),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );
    final result = jsonDecode(response.body)['data'] as Map<String, dynamic>;
    result.forEach((key, value) {
      DataKuliahModel dataKuliah = DataKuliahModel.fromMap(value);
      finalResult.addAll({
        key: dataKuliah,
      });
    });
    return finalResult;
  }

CodePudding user response:

Have you looked at the Uri replace method?

You can do the following:

Uri.parse('$url/auth/mhs_siakad/perwalian/get_paket').replace(queryParameters:{ "smt":"$smt"});  

Update on FutureBuilder:

// Put this outside your build function
Future<Map<String, DataKuliahModel>> DK ; 

// Put this in your initState if you want the future to run on page load or use it for events like onTap 
DK = Service.getDataKuliah(<PARAM>); 

// This is in your build method
FutureBuilder(

future:DK,
builder: (context, snapshot) {
// add wigets to display results here

}

)
  • Related