[英]new to flutter. I'm want to go to a webapi based on url passed not hard code path
I'm working with web API's and need to pass in a api url that I retrieved to fetch some data then display the data. 我正在使用Web API,需要传递我检索到的api网址以获取一些数据,然后显示数据。 I am able to create a listview from a api url that's hard coded, but don't know how to send in a parameter to display info based on that selected url.
我能够从经过硬编码的api网址创建一个listview,但是不知道如何发送参数来显示基于所选url的信息。 How do send in the url fetch the data and display it
如何发送网址以获取数据并显示
----------api_data_details.dart file--------- ---------- api_data_details.dart文件---------
class PokemonRepository {
//want to pass in a url
final String url ;
PokemonRepository({this.url});
final JsonDecoder _decoder = new JsonDecoder();
Future<Pokemon> fetch() async{
final detailsresponse = await http.get(url);
final jsonBody = detailsresponse.body;
final statusCode = detailsresponse.statusCode;
if(statusCode < 200 || statusCode >= 300 || jsonBody == null) {
throw new FetchDataException("Error while getting pokemon details [StatusCode:$statusCode, Error:${detailsresponse.reasonPhrase}]");
}
final pokemonContainer = _decoder.convert(jsonBody);
final pokemonDetails = pokemonContainer['results'];
return pokemonDetails.map( (pokemondetails) =>
Pokemon.fromMap(pokemondetails)) ;
}
--------------api_data.dart file-------------- -------------- api_data.dart文件--------------
class Pokemon{
final int id;
final String name;
final int height;
final List<Moves> moves;
final String imageUrl;
final int weight;
const Pokemon({this.id,this.name,this.height, this.weight,this.imageUrl,this.moves});
Pokemon.fromMap(Map<String, dynamic> map):
id = map['id'],
name = map['name'],
height = map['height'],
imageUrl = map['sprites']['front-default'],
weight = map['weight'],
moves = <Moves>[
new Moves(name: map['name'])
];
}
class Moves {
final String name;
const Moves({this.name});
}
abstract class PokemonDetailRepository{
Future<Pokemon> fetch();
}
---------------pokemon_view.dart file---------------- --------------- pokemon_view.dart文件----------------
class _PokemonListItem extends ListTile {
_PokemonListItem(PokemonList pokemonlist):
super(
title : Text(pokemonlist.name),
subtitle: InkWell(
child: Text(pokemonlist.url, style: TextStyle(fontSize: 8.0),),
onTap: (){
//want to click on url and pass it to the PokemonRepository class to retrieve the new data and then open a new page with the details from the new request
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => PokemonDetailPage( pokemonlist.url,),
}
)
);
Can't you just pass the url in to the fetch
function as a parameter? 您不能仅将URL作为参数传递给
fetch
函数吗? So the signature for fetch
becomes: 因此,
fetch
的签名变为:
Future<Pokemon> fetch(String url) {
// do stuff
}
If you want to pass a default value, the syntax is a little different: 如果要传递默认值,则语法略有不同:
Future<Pokemon> fetch({String url = "http://default"}) {
// do stuff
}
And then the caller can choose to pass a url or not. 然后,呼叫者可以选择是否传递URL。
Remember that Flutter is written in Dart, so ultimately you're just writing dart code. 请记住,Flutter是用Dart编写的,因此最终您只是在编写Dart代码。 For language-related questions (like how to pass parameters to functions) the language tour is an excellent resource.
对于与语言相关的问题(例如如何将参数传递给函数), 语言导览是一个很好的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.