简体   繁体   English

扑朔迷离。 我想转到基于URL传递的webapi而不是硬代码路径

[英]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.

相关问题 我想在 flutter 中使按钮无需点击即可自动点击。 我也在使用 WebView flutter: - I want to make a button automatically clickable without a tap in flutter. I'm also using WebView flutter: 我想要的值通过值而不是 Flutter 中的引用传递给 function。 如何避免这种情况? - The value I want is passed to the function by value rather than reference in Flutter. How to avoid this? 我想在容器中显示过滤器列表,但它在 flutter 中作为新屏幕打开。 我该怎么做? - I want to show filter list in a container but its opening as a new screen in flutter. how i can do it? Flutter.路径缩放 - Flutter. Path scaling 什么是 flutter 中 textTheme 的替代品。 我收到以下错误 - what is replacement for textTheme in flutter. I'm getting following error 我在一个文件中编写了很多代码,我想将其转移到另一个文件中,并且仍然可以在 flutter 中顺利运行。 可能吗? - I have written a lot of code in one file I want to transfer it to another file and still work smoothly in flutter. Is it possible? 我是 Flutter 的新手。我想将 JSON 数据显示为 flutter 中的列表,但我不知道如何编码以将 JSON 数据显示为 Flutter 中的列表 - I am New in Flutter. I want to display JSON Data as List in flutter but I do not know how to Coding for show JSON data as a List in Flutter 我从 GitHub 克隆的 Flutter 应用程序无法运行并显示这些错误。 我是flutter的新手,我把Sdk的版本改成30、31还是不行 - My Flutter app cloned from the GitHub doesn't run and show these errors. I'm new to flutter. I changed Sdk version to 30, 31 but it didn't work 我想构建一个可以在 flutter 中访问硬键按钮的应用程序。 我该如何实施? - I wanted to build an app that can access hard keys button in flutter. how can I implement it? Flutter - 我想检测是否在 TextField 中输入了任何 URL,如果输入了,我想在旅途中显示 URL 的预览 - Flutter - I want to detect if any URL is entered in the TextField and want to display the preview of the URL on the go if entered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM