![](/img/trans.png)
[英]Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'String' : Flutter Exception
[英]Flutter Error : Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Null>?
我正在關注 Max 的 Flutter 教程,並且在處理 http 請求時遇到錯誤部分:
我正在從 URL 中刪除“.json”以創建錯誤,但它沒有正確處理錯誤對話框顯示&當按下平面按鈕時它的功能(pop),但它仍然保留在 CircularProgressIndicator 頁面上(加載/微調頁面)
編輯1:我正在談論的錯誤不是missing.json,我正在刪除.json 以查看我的代碼如何解決錯誤。
我的代碼:
在 8,577 毫秒內重新啟動應用程序。 在 1,869 毫秒內重新加載了 634 個庫中的 0 個。 E/flutter(3845):[錯誤:flutter/lib/ui/ui_dart_state.cc(177)]未處理的異常:類型“未來”不是“未來”類型的子類型? E/flutter (3845): #0 _FutureListener.handleError (dart:async/future_impl.dart:160:20) E/flutter (3845): #1
Future._propagateToListeners.handleError (dart:async/future_impl.dart:708:47) E/flutter (3845): #2
Future._propagateToListeners (dart:async/future_impl.dart:729:24) E/flutter (3845): #3 Future._completeWithValue (dart:async/future_impl.ZBB14127678960FAE97D8739:50EA20156Z:382:50EA20156Z:3824:9)#4/flutter
_AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:40:15) E/flutter (3845): #5
_completeOnAsyncReturn (dart:async-patch/async_patch.dart:311:13) E/flutter ( 3845): #6 _withClient (package:http/http.dart) package:http/http.dart:1 E/flutter ( 3845) : #7 _rootRunUnary (dart:async/zone.dart:1198:47)
./providers/products_provider.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import './product.dart';
import 'package:http/http.dart' as http;
class Products with ChangeNotifier {
List<Product> _items = [
Product(
id: 'p1',
title: 'Red Shirt',
description: 'A red shirt - it is pretty red!',
price: 29.99,
imageUrl:
'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
),
];
List<Product> get items {
return [..._items];
}
List<Product> get favsItems {
return _items.where((element) => element.isFavourite).toList();
}
Future<void> addProduct(Product prod) {
const url =
'https://learning-5048e-default-rtdb.firebaseio.com/products';
return http
.post(url,
body: json.encode({
'title': prod.title,
'description': prod.description,
'price': prod.price,
'imageUrl': prod.imageUrl,
'isFavourite': prod.isFavourite
}))
.then((response) {
final newProduct = Product(
id: json.decode(response.body)['name'],
title: prod.title,
description: prod.description,
price: prod.price,
imageUrl: prod.imageUrl);
_items.add(newProduct);
notifyListeners();
}).catchError((error){
throw error;
});
}
void updateProduct(String id, Product xxx) {
final productIndex = _items.indexWhere((element) => element.id == id);
_items[productIndex] = xxx;
notifyListeners();
}
void deleteProduct(String id) {
_items.removeWhere((element) => element.id == id);
notifyListeners();
}
Product findById(productId) {
return _items.firstWhere((element) => element.id == productId);
}
}
./screens/edit_product_screen.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/product.dart';
import '../providers/products_provider.dart';
class EditProductScreen extends StatefulWidget {
static const routeName = '/edit-product';
@override
_EditProductScreenState createState() => _EditProductScreenState();
}
class _EditProductScreenState extends State<EditProductScreen> {
final _priceFocusNode = FocusNode();
final _describtionFocusNode = FocusNode();
final _imageEditingController = TextEditingController();
final _imageUrlFocusNode = FocusNode();
final _form = GlobalKey<FormState>();
var _editedProduct =
Product(id: null, title: '', description: '', price: 0, imageUrl: '');
var _initVals = {
'title ': '',
'description': '',
'price': '',
'imageUrl': ''
};
var _isinit = true;
var _isLoading = false;
@override
void initState() {
_imageUrlFocusNode.addListener(_updateImage);
super.initState();
}
@override
void didChangeDependencies() {
if (_isinit) {
final productId = ModalRoute.of(context).settings.arguments as String;
if (productId != null) {
_editedProduct =
Provider.of<Products>(context, listen: false).findById(productId);
print(_editedProduct.id);
_initVals = {
'title': _editedProduct.title,
'description': _editedProduct.description,
'price': _editedProduct.price.toString(),
// 'imageUrl': _editedProduct.imageUrl
// 'imageUrl': ''
};
_imageEditingController.text = _editedProduct.imageUrl;
}
}
_isinit = false;
super.didChangeDependencies();
}
@override
void dispose() {
_imageUrlFocusNode.removeListener(_updateImage);
_priceFocusNode.dispose();
_describtionFocusNode.dispose();
_imageEditingController.dispose();
_imageUrlFocusNode.dispose();
super.dispose();
}
void _updateImage() {
if (!_imageEditingController.text.endsWith('jpg') &&
!_imageEditingController.text.endsWith('png') &
!_imageEditingController.text.startsWith('http') &&
!_imageEditingController.text.startsWith('https')) {
return;
}
if (!_imageUrlFocusNode.hasFocus) {
setState(() {});
}
}
void saveForm() {
// final isValid = _form.currentState.validate();
if (!_form.currentState.validate()) {
return;
}
_form.currentState.save();
setState(() {
_isLoading = true;
});
if (_editedProduct.id != null) {
Provider.of<Products>(context, listen: false)
.updateProduct(_editedProduct.id, _editedProduct);
setState(() {
_isLoading = false;
});
Navigator.of(context).pop();
} else {
Provider.of<Products>(context, listen: false)
.addProduct(_editedProduct)
.catchError((onError) {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text('Error!'),
content: Text('Error Occured!'),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text('OK'))
],
));
}).then((_) {
setState(() {
_isLoading = false;
});
Navigator.of(context).pop();
});
}
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Product'),
actions: [IconButton(icon: Icon(Icons.save), onPressed: saveForm)],
),
body: _isLoading
? Center(child: CircularProgressIndicator())
: Padding(
padding: EdgeInsets.all(15),
child: Form(
key: _form,
child: ListView(children: [
TextFormField(
initialValue: _initVals['title'],
decoration: InputDecoration(labelText: 'Title'),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_priceFocusNode);
},
onSaved: (val) {
_editedProduct = Product(
id: _editedProduct.id,
title: val,
description: _editedProduct.description,
price: _editedProduct.price,
imageUrl: _editedProduct.imageUrl,
isFavourite: _editedProduct.isFavourite,
);
},
validator: (value) {
if (value.isEmpty) {
return 'Please Provide A Valid Title Name';
}
return null;
},
),
TextFormField(
initialValue: _initVals['price'],
decoration: InputDecoration(labelText: 'Price'),
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
focusNode: _priceFocusNode,
onFieldSubmitted: (_) {
Focus.of(context).requestFocus(_describtionFocusNode);
},
onSaved: (val) {
_editedProduct = Product(
id: _editedProduct.id,
title: _editedProduct.title,
description: _editedProduct.description,
price: double.parse(val),
imageUrl: _editedProduct.imageUrl,
isFavourite: _editedProduct.isFavourite,
);
},
validator: (value) {
if (value.isEmpty) {
return 'Please Enter A Price';
}
if (double.tryParse(value) == null ||
double.tryParse(value) <= 0) {
return 'Please Enter A Valid Price';
}
return null;
},
),
TextFormField(
initialValue: _initVals['description'],
decoration: InputDecoration(labelText: 'Description'),
keyboardType: TextInputType.text,
maxLines: 3,
focusNode: _describtionFocusNode,
onSaved: (val) {
_editedProduct = Product(
id: _editedProduct.id,
title: _editedProduct.title,
description: val,
price: _editedProduct.price,
imageUrl: _editedProduct.imageUrl,
isFavourite: _editedProduct.isFavourite,
);
},
validator: (value) {
if (value.isEmpty) {
return 'Please Enter A Valid Description';
}
return null;
},
),
Row(crossAxisAlignment: CrossAxisAlignment.end, children: [
Container(
child: _imageEditingController.text.isEmpty
? Text('Insert Image')
: FittedBox(
fit: BoxFit.cover,
child: Image.network(
_imageEditingController.text)),
margin: EdgeInsets.only(top: 10, right: 10),
width: 100,
height: 100,
decoration: BoxDecoration(border: Border.all(width: 1)),
),
Expanded(
child: TextFormField(
// initialValue: _initVals['imageUrl'],
decoration: InputDecoration(labelText: 'Image URL'),
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
controller: _imageEditingController,
focusNode: _imageUrlFocusNode,
onFieldSubmitted: (_) {
saveForm();
},
onSaved: (value) {
_editedProduct = Product(
id: _editedProduct.id,
title: _editedProduct.title,
description: _editedProduct.description,
price: _editedProduct.price,
imageUrl: value,
isFavourite: _editedProduct.isFavourite,
);
},
validator: (value) {
if (value.isEmpty) {
return 'Please Enter A Valid Image URL';
}
if (!value.endsWith('jpg') &&
!value.endsWith('png') &&
!value.endsWith('bmp')) {
return 'Please Enter A valid Url For The Image';
}
return null;
},
),
)
])
])),
),
);
}
}
向此 URL 發出 POST 請求
https://learning-5048e-default-rtdb.firebaseio.com/products
回應是:
append .json to your request URI to use the REST API.
接下來,jsonDecode 嘗試解碼此響應,並在失敗時拋出FormatException ,因為很明顯,它不是 JSON 格式。
似乎 URL 本身有故障。 正如響應所暗示的,在 URL 的末尾添加.json:
https://learning-5048e-default-rtdb.firebaseio.com/products.json
我試過了,當發出帶有正文的 POST 請求時,它會返回一些 json 響應,這可能是您正在尋找的。
好吧,在課程的下一個視頻中,他說明了(try catch)方法,我修改了代碼,它按預期工作:D!
不知道之前代碼的錯誤修復,可能是dart較新版本的代碼支持,可能是一些舊方法,我不知道
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.