我想从Firebase数据库检索数据以将其插入到我的MultiSelectFormField类型表单中时遇到问题。当我单击表单时,列表不会打开。错误如下:
[[ERROR:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常:类型'String'不是'index'E / flutter类型'int'的子类型(27777):#0新的MultiSelectFormField ...(包:multiselect_formfield / multiselect_formfield.dart:73:55)E / flutter(27777):在每个列表中排名第一(dart:core-patch / growable_array.dart:285:8)E / flutter(27777):#2新的MultiSelectFormField ..(package:multiselect_formfield / multiselect_formfield.dart:72:28)
我创建此表单的方法
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);
}
}
datasource = e.allTags.map((data){
return {'display': '#$data', 'value': data};
}).toList(),
应该工作。
作为数据源保存类型为List
(documentation)的数据。
您的代码:
e.allTags.map((data){
return {'display': '#$data', 'value': data};
}).toList(),
本身创建一个List
,其中包含allTags >>]的每个元素