dart开发Android服务,关于android:在Flutter应用中使用由swagger生成的Dart代码生成的Web服务...
我正在嘗試使用Flutter開發一個移動應用程序,我使用swagger生成了包含所有Web服務的Dart文件代碼生成。我想從Web服務中獲取所有用戶的列表。 在屏幕上,我想為每個用戶顯示:圖像,名字,姓氏和電子郵件。 我已經在main.dart中準備了如下的UI:
import 'package:flutter/material.dart';
import './utility.dart';
void main() => runApp(ListUserApp());
class ListUserApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'User List 4Motors',
home: ListUserScreen(),
);
}
}
class ListUserScreen extends StatefulWidget {
@override
State createState() {
return ListUserScreenState();
}
}
class ListUserScreenState extends State {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.indigo,
),
home: Scaffold(
appBar: AppBar(
title: Text('User List 4Motors'),
),
body: _buildListUser(),
),
);
}
Widget _buildListUser() {
Utility test = new Utility();
print(test.getFirstNameUser());
return ListView.builder(
itemBuilder: (context, position) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
margin: const EdgeInsets.all(10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(right: 15.0),
child: Image(
width: 65, image: AssetImage('assets/person.jpeg')), ?// Image of user
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'firstname & lastname', // first and last name of user
style: TextStyle(
fontSize: 22,
),
),
Container(
margin: const EdgeInsets.all(5.0),
child: Text('email'), // Email of user
),
],
),
],
),
),
),
);
});
}
}
并且,以下由swagger生成的用戶模型:
part of swagger.api;
class UsersData {
String id = null;
String firstName = null;
String lastName = null;
String email = null;
String phone = null;
String image = null;
DateTime birthDay = null;
String fireBaseID = null;
String dealerID = null;
String type = null;
String provider = null;
DateTime registrationDate = null;
DateTime lastLogin = null;
bool allowComment = null;
bool isActive = null;
List addresses = [];
UsersData();
@override
String toString() {
return 'UsersData[id=$id, firstName=$firstName, lastName=$lastName, email=$email, phone=$phone, image=$image, birthDay=$birthDay, fireBaseID=$fireBaseID, dealerID=$dealerID, type=$type, provider=$provider, registrationDate=$registrationDate, lastLogin=$lastLogin, allowComment=$allowComment, isActive=$isActive, addresses=$addresses, ]';
}
UsersData.fromJson(Map json) {
if (json == null) return;
id = json['id'];
firstName = json['firstName'];
lastName = json['lastName'];
email = json['email'];
phone = json['phone'];
image = json['image'];
birthDay =
json['birthDay'] == null ? null : DateTime.parse(json['birthDay']);
fireBaseID = json['fireBaseID'];
dealerID = json['dealerID'];
type = json['type'];
provider = json['provider'];
registrationDate = json['registrationDate'] == null
? null
: DateTime.parse(json['registrationDate']);
lastLogin =
json['lastLogin'] == null ? null : DateTime.parse(json['lastLogin']);
allowComment = json['allowComment'];
isActive = json['isActive'];
addresses = UserAddressData.listFromJson(json['addresses']);
}
Map toJson() {
return {
'id': id,
'firstName': firstName,
'lastName': lastName,
'email': email,
'phone': phone,
'image': image,
'birthDay': birthDay == null ? '' : birthDay.toUtc().toIso8601String(),
'fireBaseID': fireBaseID,
'dealerID': dealerID,
'type': type,
'provider': provider,
'registrationDate': registrationDate == null
? ''
: registrationDate.toUtc().toIso8601String(),
'lastLogin': lastLogin == null ? '' : lastLogin.toUtc().toIso8601String(),
'allowComment': allowComment,
'isActive': isActive,
'addresses': addresses
};
}
static List listFromJson(List json) {
return json == null
? new List()
: json.map((value) => new UsersData.fromJson(value)).toList();
}
static Map mapFromJson(
Map> json) {
var map = new Map();
if (json != null && json.length > 0) {
json.forEach((String key, Map value) =>
map[key] = new UsersData.fromJson(value));
}
return map;
}
}
我創建了一個" Utility.dart"類,該類中放置了一種獲取內部所有用戶的名字的列表的方法,如下所示:
import 'package:flutter_app_ws/dart-client-generated/lib/api.dart';
class Utility {
UsersData user;
Utility();
List getFirstNameUser() {
List firstName = new List();
firstName.add(user.firstName);
return firstName;
}
}
當我運行我的應用程序時,出現很多錯誤,如下所示:
Compiler message:
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:6:8:
Error: Not found: 'dart:html'
import 'dart:html';
^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:95:25:
Error: Type 'HttpRequest' not found.
void _openHttpRequest(HttpRequest request, String method, String url,
^^^^^^^^^^^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:30:25:
Error: 'HttpRequest' isn't a type.
final _xhrs = new Set();
^^^^^^^^^^^
lib/main.dart:63:27: Error: Expected an identifier, but got ','.
, // first and last name of user
^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:44:19:
Error: Method not found: 'HttpRequest'.
var xhr = new HttpRequest();
^^^^^^^^^^^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:55:45:
Error: Method not found: 'Blob'.
var blob = xhr.response == null ? new Blob([]) : xhr.response;
^^^^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:56:24:
Error: Method not found: 'FileReader'.
var reader = new FileReader();
^^^^^^^^^^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:55:49:
Error: Too many positional arguments: 0 allowed, but 1 found.
Try removing the extra positional arguments.
var blob = xhr.response == null ? new Blob([]) : xhr.response;
^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:95:25:
Error: 'HttpRequest' isn't a type.
void _openHttpRequest(HttpRequest request, String method, String url,
^^^^^^^^^^^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:97:13:
Error: The method 'open' isn't defined for the class 'invalid-type'.
Try correcting the name to the name of an existing method, or defining a method named 'open'.
request.open(method, url, async: asynch, user: user, password: password);
^^^^
file:///home/innovi/development/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib/src/browser_client.dart:105:11:
Error: The method 'abort' isn't defined for the class 'invalid-type'.
Try correcting the name to the name of an existing method, or defining a method named 'abort'.
xhr.abort();
我想知道問題所在,以及如何使用我的Web服務來獲取和顯示:所有用戶的圖像,名字/姓氏和電子郵件。
也許您在pubspec.yaml中缺少某些軟件包。 招搖是否在生成過程中生成了pubspec.yaml? 在這種情況下,您可以將所需的包復制并粘貼到主項目pubspec.yaml中。
我可以使用2.4.2版的swagger-codgen生成用于測試flutter項目的swagger客戶端,它應該已經解決了該問題。
java -jar swagger-codegen-cli-2.4.2.jar generate -l dart -i openapi.json -o swagger -DbrowserClient=false
重要標志:-DbrowserClient=false
然后按照README.md指令將生成的swagger庫添加到我的測試flutter項目中:
Local
To use the package in your local drive, please include the following in >pubspec.yaml
dependencies:
swagger:
path: /path/to/swagger
Tests
TODO
Getting Started
Please follow the installation procedure and then run the following:
import 'package:swagger/api.dart';
// TODO Configure API key authorization: api_key
//swagger.api.Configuration.apiKey{'key'} = 'YOUR_API_KEY';
// uncomment below to setup prefix (e.g. Bearer) for API key, if needed
//swagger.api.Configuration.apiKeyPrefix{'key'} ="Bearer";
var api_instance = new DefaultApi();
我只需要在swagger庫的pubspec.yaml中也明確指定環境。
name: swagger
version: 1.0.0
description: Swagger API client
environment:
sdk:">=2.1.0 <3.0.0"
dependencies:
http: '>=0.11.1 <0.12.0'
更新
我也嘗試過openapi-generator-cli
java -jar openapi-generator-cli-3.3.4.jar generate -l dart -i openapi.json -o openapi -DbrowserClient=false
和Followwing README.md的方式與您大張旗鼓一樣。
我嘗試了,兩種解決方案都可以。 開放的API似乎比搖搖欲墜的客戶端更撲朔迷離,因為我不需要在生成的開放api庫的pubspec.yaml中添加環境,但是它是自動設置的。
是的,swagger還會生成一個pubspec.yaml,其中包含以下幾行"名稱:swagger版本:1.0.0說明:Swagger API客戶端依賴項:http:> = 0.11.1 <0.12.0",我添加了缺少的依賴項"達特森:" ^ 0.2.4" dev_dependencies:吉尼斯:^ 0.1.17瀏覽器:任何轉換器:-達特森",但錯誤仍然存??在
我迷路了,有人可以幫我嗎
@MimiSoftware發現了此問題。因此,我將嘗試使用最新的代碼生成,然后讓您知道。
當我輸入以下命令時:" java -jar swagger-codegen-cli-2.4.2.jar generate -l dart -i openapi.json -o swagger -DbrowserClient = false",以下內容顯示為"無法訪問jarfile swagger-codegen- cli-2.4.2.jar"
@MimiSoftware您需要從這里下載它...
完成,但出現另一個錯誤"線程"主"中的異常java.lang.UnsupportedClassVersionError:io / swagger / codegen / SwaggerCodegen:不支持的major.minor版本51.0"
@MimiSoftware github.com/swagger-api/swagger-codegen/issues/605,github.com/swagger-api/swagger-codegen/issues/917我的Java版本是java version"1.8.0_191"
@MimiSoftware Ive已考慮開放API生成器更新了我的答案。順便說一句,這兩種解決方案都對我有效。
我將Java版本升級到" 1.8.0_171",然后通過此命令" wget central.maven.org/maven2/org/openapitools/openapi-generator-cli/-下載了openapi-generator-cli-3.3.4.jar- O openapi-generator-cli.jar",之后我運行此命令" java -jar openapi-generator-cli.jar generate -l dart -i openapi.json -o openapi -DbrowserClient = false",出現如下錯誤" [錯誤]找不到規范文件:openapi.json [錯誤]檢查OpenAPI規范的路徑,然后重試。
您是如何生成您的swagger庫的第一個版本的?我猜您沒有使用搖搖欲墜的代碼生成的jar版本。我曾經使用過一個,而我在這里也曾經使用過。如果您對這種類型的生成不滿意,則仍然可以使用實際方法,但請記住不要使用此標志-DbrowserClient=false生成客戶端瀏覽器。 openapi.json是您應該擁有的文件,通常是后端開發人員提供的文件,并且是(通過開放api規范)描述WebService的文件,并且是生成客戶端所需的文件。
@MimiSoftware由于您已經擁有客戶端庫的代碼,因此僅生成一次,那么如何生成它呢?粗俗的原因,您沒有使用我在這里建議您的方法...
@MimiSoftware因此,選擇了您希望生成代碼的方法,但是使用此標志-DbrowserClient=false.以便不生成ClientBrowser,該組件使您的代碼無法編譯。
我只是去" swagger.io",然后用帳戶登錄以搖晃集線器,然后選擇" import Api",然后輸入我的Web服務的網址。之后,我將此文件導出為" dart-client-generation" dart文件。最后,我將此文件導入flutter項目中,并在pubspec.yaml中添加了缺少的依賴項,這是我遵循的過程。你明白我了嗎 ?
@MimiSoftware是的,我了解您,但我從未使用過"導入API"功能,因為我沒有SwaggerHub帳戶。但是,如果您的Web服務與OpenApi兼容,則可以使用yaml或json格式獲得Web服務規范。通常是通過附加諸如example.comswagger之類的快捷路徑來提供的Web服務URL,但這取決于您的配置。因此,您可以檢查如何在SwaggerHub(我不知道這部分)中傳遞一個生成標記,或者獲取您的Web服務規范并使用我的方法。
這是我工作中必需的方法。 到目前為止,我對如何使用Web服務一無所知。 無論如何,謝謝您的所有答復,如果您遇到有關我的問題的一些事情,請告訴我。
總結
以上是生活随笔為你收集整理的dart开发Android服务,关于android:在Flutter应用中使用由swagger生成的Dart代码生成的Web服务...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (DFS)棋盘问题(poj1321)
- 下一篇: java 内存泄露 书籍_java虚拟机