当我在Flutter中集成我的ASP.NET Web API时,在Chrome调试器上运行后会显示以下错误。
dartUri: Unresolved uri: dart:web_sql
dartUri: Unresolved uri: dart:ui
**注:**我运行此代码在VS代码。当我运行此代码在其默认的windows调试器它不显示任何错误。它也显示数据库中的数据。但它不能插入数据库中的数据。当相同的代码我运行在Chrome调试器它会显示上述错误。
main.dart代码:
import 'package:api_call/API%20-%201/ui/BooksUI.dart';
import 'package:api_call/API-Sr-Umar/Api_Config.dart';
import 'package:api_call/ApiCall.dart';
import 'package:flutter/material.dart';
void main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: ApiConfig(),
);
}
}
Api配置dart代码
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class ApiConfig extends StatefulWidget {
const ApiConfig({Key? key}) : super(key: key);
@override
State<ApiConfig> createState() => _ApiConfigState();
}
class _ApiConfigState extends State<ApiConfig> {
List<Books> bdata = [];
Future<void> AddBook() async {
print('Add Book Called');
var response = await http.post(Uri.parse("http://192.168.56.1/MyFirstApi/api/TestApi/AddBook"),
headers: <String, String>{'Content-Type': 'application/json; charset=UTF-8'},
body: jsonEncode(<String, dynamic>{
"ID": 1,
"BNAME": "BIO",
"BPRICE": 180,
"BPUBYEAR": "2001",
}));
print(response.body);
if (response.statusCode == 200) {
print('Inserted');
} else {
print('Insertion Failed');
}
}
Future<void> getAllBooks() async {
var response = await http.get(Uri.parse('http://192.168.56.1/MyFirstApi/api/TestApi/getAllBooks'));
if (response.statusCode == 200) {
print('Successfully Called');
print(response.body);
print('Decoding');
Iterable listt = jsonDecode(response.body);
bdata = listt.map((e) => Books.fromMap(e)).toList();
setState(() {
// bdata;
});
} else {
print('Fetching Failed');
}
}
TextEditingController id = TextEditingController();
TextEditingController bname = TextEditingController();
TextEditingController bprice = TextEditingController();
TextEditingController bpubyear = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Api Config'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
myTextField(id, 'Enter Id'),
myTextField(bname, 'Enter Name'),
myTextField(bprice, 'Enter Price'),
myTextField(bpubyear, 'Enter Publish Year'),
ElevatedButton(
onPressed: () {
AddBook();
},
child: Text("Add Book")),
ElevatedButton(
onPressed: () {
getAllBooks();
},
child: Text("Get All Books")),
ListView.builder(
scrollDirection: Axis.vertical,
itemCount: bdata.length,
itemBuilder: (context, index) {
return Text(bdata[index].id.toString() +
" " +
bdata[index].bname.toString() +
" " +
bdata[index].bprice.toString() +
" " +
bdata[index].bpubyear.toString());
},
shrinkWrap: true,
)
],
),
);
}
}
TextField myTextField(TextEditingController ctrl, String htext) {
return TextField(
decoration: InputDecoration(hintText: htext),
controller: ctrl,
);
}
class BooksRecord {
late int id;
late String bname;
late int bprice;
late int bpubyear;
BooksRecord(this.id, this.bname, this.bprice, this.bpubyear);
BooksRecord.fromMap(Map<String, dynamic> mp) {
id = mp['ID'];
bname = mp['BNAME'];
bprice = mp['BPRICE'];
bpubyear = mp['BPUBYEAR'];
}
}
class Books {
late int id;
late String bname;
late int bprice;
late int bpubyear;
Books(this.id, this.bname, this.bprice, this.bpubyear);
int getID() => id;
String getBName() => bname;
int getBPrice() => bprice;
int getBPubYear() => bpubyear;
Map<String, dynamic> toMap() {
Map<String, dynamic> mp = Map<String, dynamic>();
mp['ID'] = id;
mp['BNAME'] = bname;
mp['BPRICE'] = bprice;
mp['BPUBYEAR'] = bpubyear;
return mp;
}
Books.fromMap(Map<String, dynamic> map) {
id = map['ID'];
bname = map['BNAME'];
bprice = map['BPRICE'];
bpubyear = map['BPUBYEAR'];
}
}
帮助我解决此错误
在默认Windows调试器中运行的图片:
在WINDOWS调试器中单击添加书籍错误:
flutter: Add Book Called
flutter: "An error occurred while updating the entries. See the inner exception for details."
flutter: Insertion Failed
在chrome调试器中运行相同代码时:
dartUri: Unresolved uri: dart:web_sql
dartUri: Unresolved uri: dart:ui
1条答案
按热度按时间vlju58qv1#
这是dart-lang/webdev中影响Flutter的一个问题。在Dart webdev上的更新也推出到Flutter后,这个问题得到了修复。