当我想从firebase数据库中检索数据以将其插入MultiSelectFormField类型表单时,遇到了一个问题。当我单击表单时,列表不会打开。错误如下:
错误:未处理的异常:类型"String"不是"index"E/flutter(27777(的类型"int"的子类型:#0 new MultiSelectFormField。。。(软件包:multiselect_formfield/multiselect_formfield.dart:73:55(E/flutter(27777(:#1
谢谢你的回复。
在这里检查我的代码:
///my method to build this form///////////
Widget buildMultiForm(BuildContext context,EndRegisterViewModel e) {
return MultiSelectFormField(
autovalidate: e.autoValidateTag,
titleText: 'Choisissez les dispositifs qui vous intéressent',
validator: (value)
{
if (e.selectedsValueTag == null || e.selectedsValueTag.length == 0) {
return 'Veuillez sélectionner au moins un tag';
}
return null;
},
dataSource: [
e.allTags.map((data){
return {'display': '#$data', 'value': data};
}).toList(),
],
textField: 'display',
valueField: 'value',
okButtonLabel: 'OK',
cancelButtonLabel: 'Annuler',
hintText: 'Choisissez-en un ou plus',
value: e.selectedsValueTag,
onSaved: (value) {
e.autoValidateTag=true;
e.selectedsValueTag = value;
},
);
}
/////////////////////////////////////////////
/////User Model ////////////////////////////
////////////////////////////////////////////
class User {
String uid;
String name;
String surname;
String email;
String city;
String country;
int experience;
String numberTel;
String department;
/// List<String> tags;
String tags;
String reason;
List pictures;
bool isRegistered;
List confirmedMatches;
List receivedMatches;
List sendMatches;
///USER FROM DATA////
/// /// /// //// ///
User.fromData(Map<String, dynamic> data)
: uid = data['uid'],
email = data['email'],
name = data['name'],
surname = data['surname'],
city = data['city'],
country = data['country'],
experience = data['experience'],
numberTel = data['numberTel'],
department = data['department'],
tags = data['tags'],
reason = data['reason'],
pictures = data['pictures'] as List,
isRegistered = data['isRegistered'] as bool ?? false,
confirmedMatches = data['confirmedMatches'] as List,
receivedMatches = data['receivedMatches'] as List,
sendMatches = data['sendMatches'] as List;
Map<String, dynamic> toJson() {
return {
'uid': uid,
'email': email,
'name': name,
'surname': surname,
'city': city,
'country': country,
'experience': experience,
'numberTel': numberTel,
'department': department,
'tags': tags,
'pictures': pictures,
'isRegistered': isRegistered,
'reason':reason,
'confirmedMatches': confirmedMatches,
'receivedMatches': receivedMatches,
'sendMatches': sendMatches,
};
}
////ViewModel//////
//////////////////
////////////////
class EndRegisterViewModel extends BaseModel {
final FirestoreService _firestoreService = locator<FirestoreService>();
final NavigationService _navigationService = locator<NavigationService>();
final AuthService _auth = locator<AuthService>();
final formKey = new GlobalKey<FormState>();
List<String> _allDepartments;
List<String> _allTags;
List<String> get allDepartments => _allDepartments;
List<String> get allTags => _allTags;
List _pictures;
String _name;
String _surname;
String _reason;
String _numberTel;
String _errorMessage;
List _selectedsValueTag;
bool _autoValidateTag = false;
bool get autoValidateTag => _autoValidateTag;
set autoValidateTag(bool newValue) {
if (autoValidateTag != newValue) {
_autoValidateTag = newValue;
notifyListeners();
}
}
List get selectedsValueTag => _selectedsValueTag;
set selectedsValueTag(List newValue) {
if (selectedsValueTag != newValue) {
_selectedsValueTag = newValue;
notifyListeners();
}
}
String _selectedValueDepartment;
String get selectedValueDepartment => _selectedValueDepartment;
set selectedValueDepartment(String newValue) {
if (selectedValueDepartment != newValue) {
_selectedValueDepartment = newValue;
notifyListeners();
}
}
/// CONSTRUCTOR ///
/// /// /// /// ////
EndRegisterViewModel() {
_allDepartments = new List<String>();
_allTags = new List<String>();
}
set pictures(List value) {
_pictures = value;
}
set name(String value) {
_name = value;
}
set surname(String value) {
_surname = value;
}
set reason(String value) {
_reason = value;
}
set numberTel(String value) {
_numberTel = value;
}
String get errorMessage => _errorMessage;
String get reason => _reason;
bool validateAndSave() {
final form = formKey.currentState;
if (form.validate()) {
form.save();
return true;
}
return false;
}
void setErrorMessage(String errorMessage) {
_errorMessage = errorMessage;
notifyListeners();
}
Future updateUser() async {
setBusy(true);
if (validateAndSave()) {
User _user = _auth.currentUser;
_user.name = _name;
_user.surname = _surname;
_user.department = _selectedValueDepartment;
_user.tags = _selectedsValueTag.toString();
_user.reason = _reason;
_user.numberTel = _numberTel;
_user.isRegistered = true;
var result = await _firestoreService.createOrUpdateUser(_user);
if (result == null) {
_auth.currentUser = _user;
_navigationService.navigateTo("/communities");
} else {
setErrorMessage(result);
setBusy(false);
}
} else {
setBusy(false);
}
}
Future<void> initialize() async {
setBusy(true);
if (_allDepartments == null || _allDepartments.isEmpty) {
_allDepartments = await _firestoreService.getAllDepartments(); /// it's work
notifyListeners();
}
if (_allTags == null || _allTags.isEmpty) {
_allTags = await _firestoreService.getAllTags();
notifyListeners();
}
setBusy(false);
}
}
我想请求您更换
return {'display': '#$data', 'value': data};
至
return {"display": "${data}", "value": "${data}"};