Home > Mobile >  How to Pass parameters in url. in flutter
How to Pass parameters in url. in flutter

Time:10-11

I want to pass some parameters in flutter url

here is example:

parameters are source , destination , type, fare.

url is http://localhost:9000/api/bmrc/fare/source/destination/fare/type

url need to send http://localhost:9000/api/bmrc/fare/1/17/2/SJT (workinng in postman/thunderclient)

I tried after passing these parameters in body but its not worked for me

CodePudding user response:

They are Path Parameters, not body JSON.

You can call your API like this:

callAPI(int source,int destination,int fare,String type){
  String _url= "http://localhost:9000/api/bmrc/fare/${source}/${destination}/${fare}/${type}";
  ... //call your API with _url
}

You can change the param types as you like

CodePudding user response:

its a POST Call: To get data from from your url, try this.

Here is the sample Example:

import 'dart:async';  
import 'dart:convert';  
  
import 'package:flutter/material.dart';  
import 'package:http/http.dart' as http;  
  
void main() => runApp(MyApp());  
  
class MyApp extends StatefulWidget {  
  MyApp({Key key}) : super(key: key);  
  
  @override  
  _MyAppState createState() => _MyAppState();  
}  
  
class _MyAppState extends State<MyApp> {  
Future<Post> post;  
  
  @override  
  void initState() {  
    super.initState();  
    post = fetchPost();  
  }  
  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      title: 'Flutter REST API Example',  
      theme: ThemeData(  
        primarySwatch: Colors.green,  
      ),  
      home: Scaffold(  
        appBar: AppBar(  
          title: Text('Flutter REST API Example'),  
        ),  
        body: Center(  
          child: FutureBuilder<Post>(  
            future: post,  
            builder: (context, abc) {  
              if (abc.hasData) {  
                return Text(abc.data.title);  
              } else if (abc.hasError) {  
                return Text("${abc.error}");  
              }  
  
              // By default, it show a loading spinner.  
              return CircularProgressIndicator();  
            },  
          ),  
        ),  
      ),  
    );  
  }  
}  
  
Future<Post> fetchPost() async {  
  final response = await http.get('Give your JSON file web link.');  
  
  if (response.statusCode == 200) {  
    // If the call to the server was successful (returns OK), parse the JSON.  
    return Post.fromJson(json.decode(response.body));  
  } else {  
    // If that call was not successful (response was unexpected), it throw an error.  
    throw Exception('Failed to load post');  
  }  
}  
  
class Post {  
  final int userId;  
  final int id;  
  final String title;  
  final String description;  
  
  Post({this.userId, this.id, this.title, this. description});  
  
  factory Post.fromJson(Map<String, dynamic> json) {  
    return Post(  
      userId: json['userId'],  
      id: json['id'],  
      title: json['title'],  
      description: json[' description'],  
    );  
  }  
}  
  • Related