我想在flutter url中傳遞一些引數
這是示例:
引數是來源、目的地、型別、票價。
網址是 http://localhost:9000/api/bmrc/fare/source/destination/fare/type
url需要發送http://localhost:9000/api/bmrc/fare/1/17/2/SJT
(在郵遞員/雷霆客戶端作業)
我在 body 中傳遞了這些引數后嘗試過,但它對我不起作用
uj5u.com熱心網友回復:
它們是路徑引數,而不是主體 JSON。
您可以像這樣呼叫 API:
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
}
您可以根據需要更改引數型別
uj5u.com熱心網友回復:
它是一個 POST 呼叫:要從您的 url 獲取資料,請嘗試此操作。
這是示例示例:
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'],
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318222.html
下一篇:無需重新加載的頁面切換
