Home > Back-end >  How to write a get function in flutter with route as well as a variable
How to write a get function in flutter with route as well as a variable

Time:11-27

so i have this function in my backend..

```
Route::get('/geteachemp/{id}', 'App\Http\Controllers\EmployeeLeaveController@geteempleave');```

where i pass in the route and an id so it only gives this that particular information with that specific id..it works well in my reactjs api made whereby i call it like this

```const getleaves = async (id) => {
        axios.get("geteachempleave/"   id).then(res => {
            if (res.status === 200) {
                setleavedata(res.data)
                //setLoading(false);
            }
        })
    };```

any idea how can i do the same ting with flutter..rn this is my get function on flutter

```_getNews() {
    CallApi().getPublicData("getempleave").then((response) {
      setState(() {
        Iterable list = json.decode(response.body);
        news = list.map((model) => Myrecord2.fromJson(model)).toList();
      });
    });
  }```

how can i also pass in id thea with the route like i did inreactjs.. Thanks

CodePudding user response:

  1. Add the http package
dependencies:
  http: <latest_version>

Import the http package.

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

Additionally, in your AndroidManifest.xml file, add the Internet permission.

<!-- Required to fetch data from the internet. -->
<uses-permission android:name="android.permission.INTERNET" />
  1. Make a network request
Future<http.Response> fetchAlbum() {
  return http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
}

if nothing of that works you might consider reading the full article.

  • Related