【问题标题】:Flutter: Setting up own Rest API json fileFlutter:设置自己的 Rest API json 文件
【发布时间】:2021-11-05 14:16:21
【问题描述】:

我正在尝试找出最好和最简单的方法来设置我自己的 json 文件并加载它,以便使用我的 Flutter 应用程序的每个人都可以访问它。我对颤振和编程比较陌生。 最好的方法是什么?我看过一些教程,这些人已经拥有来自第三方的 URL,因此他们可以轻松地在他们的应用程序上显示数据。对于我想做的事情,没有这样的API,所以我必须自己设置它并用我自己的数据填充它。 提前非常感谢您!

【问题讨论】:

  • 视情况而定。如果您希望能够更新 JSON 文件而无需推送应用程序的新版本,则需要将 JSON 文件拖放到您自己的 Web 服务器上。您可以使用Http Package 加载它,或者您想要进行联网。否则,您可以将 JSON 文件拖放到项目中的 assets 文件夹中,并将您的 pubspec.yaml 配置为使用资产。
  • 是的,我不想在每次向 json 文件中添加新数据时推送新版本的应用程序。那么没有其他方法可以设置我自己的网络服务器吗?例如,是否有某种服务提供商可以上传我的 json 文件,然后从该服务提供商获取 URL?我现在真的对安全功能一无所知,所以设置我自己的服务器可能有点困难..
  • 有可用的服务。您可以保存自己的文件并允许直接访问文件的任何地方。例如:GitHub。在公共存储库中发布您的 JSON。然后你可以使用 github 提供给原始文件的 URL。例如。 raw.githubusercontent.com/angular/angular.js/master/…
  • 非常感谢!我会尝试这个解决方案,让我们看看我是否能够做到

标签: ios node.js json flutter api


【解决方案1】:
1) Setting up the Project:
//Install the HTTP dependency and add it in pubspec.yaml file in order to use API in the application.

dependencies:
  http:
2) Creating a Request:
//This basic request uses the get method to fetch the data from the specified URL in JSON format. Each request returns a Future<Response>. A Future<> is used to represent a potential value or error that will be available at some time in the future. For example, you made a request to a server now the response will take less than a few seconds. This time is represented from Future<>. Here, we use the async & await feature which ensures that the response is asynchronous. It means that until & unless we get the response, it will not move further.

Future<List<Fruit>> fetchFruit() async {
final response = await http.get(url);
}
String url = "Your_URL";

3) Creating the Classes:
//Create a Fruit class & save it as fruit.dart as shown below:

class Fruit {
final int id;
final String title;
final String imgUrl;
final int quantity;

Fruit(
this.id,
this.title,
this.imgUrl,
this.quantity,
);
factory Fruit.fromMap(Map<String, dynamic> json) {
return Fruit(json['id'], json['title'], json['imgUrl'], json['quantity']);
}
factory Fruit.fromJson(Map<String, dynamic> json) {
return Fruit(json['id'], json['title'], json['imgUrl'], json['quantity']);
}
}

4) Create Class objects:
//Create the FruitItem in fruitItem.dart

import 'package:flutter/material.dart';
import 'fruit.dart';

class FruitItem extends StatelessWidget {
FruitItem({this.item});

final Fruit item;

Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(2),
height: 140,
child: Card(
elevation: 5,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Image.network(
this.item.imgUrl,
width: 200,
),
Expanded(
child: Container(
padding: EdgeInsets.all(5),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(this.item.title,
style: TextStyle(fontWeight: FontWeight.bold)),
Text("id:${this.item.id}"),
Text("quantity:${this.item.quantity}"),
],
)))
]),
));
}
}

5) Create a List of fruits:
//Create a FruitList class in fruitList.dart as shown below:

import 'package:flutter/material.dart';
import 'fruit.dart';
import 'fruitItem.dart';

class FruitList extends StatelessWidget {
final List<Fruit> items;

FruitList({Key key, this.items});
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return FruitItem(item: items[index]);
},
);
}
}
6) Displaying the Responses:
Display the response on the screen from main.dart file as shown below:

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'fruit.dart';
import 'fruitItem.dart';
import 'fruitList.dart';

class MyHomePage extends StatelessWidget {
final String title;
final Future<List<Fruit>> products;

MyHomePage({Key key, this.title, this.products}) : super(key: key);

@override
Widget build(BuildContext context) 
{
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF4CAF50),
title: Text("GeeksForGeeks"),
),
body: Center(
child: FutureBuilder<List<Fruit>>(
future: products,
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? FruitList(items: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
));
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 2013-04-07
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2020-09-13
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多