I'm storing my agent data in firestore. But as I want search implementation in my code. I want to search the user by its city. So i'm trying to store search index of the city. Even if I type a single alphabet for the search query I should get the most appropriate result from the firestore. How to use upload data function with my current code of adding data to firestore. I only want to use uploadData function with my city. So i can search that agent using city. I have added city controller to uploadData function on calling it. But i'm not calling it the right way. As, it displays error.
The error i got
I/flutter (25254): ...skipping image upload
E/flutter (25254): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Only valid value is 0: 1
E/flutter (25254): #0 List.[] (dart:core-patch/growable_array.dart:166:60)
E/flutter (25254): #1 _AgentSignUpState.uploadData (package:signup/agentSignup.dart:115:29)
E/flutter (25254): #2 _AgentSignUpState._showName.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:signup/agentSignup.dart:513:85)
E/flutter (25254): #3 _AgentSignUpState._showName.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:signup/agentSignup.dart:502:56)
E/flutter (25254): #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
E/flutter (25254): #5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
E/flutter (25254): #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter (25254): #7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter (25254): #8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter (25254): #9 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256:7)
E/flutter (25254): #10 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158:27)
E/flutter (25254): #11 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224:20)
E/flutter (25254): #12 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter (25254): #13 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter (25254): #14 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter (25254): #15 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter (25254): #16 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (25254): #17 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (25254): #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (25254): #19 _invoke1 (dart:ui/hooks.dart:267:10)
E/flutter (25254): #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
E/flutter (25254):
My code
uploadData(String name)async
{
List<String> splitList = name.split(' ');
List<String> indexList = [];
for(int i=0; i<splitList[i].length; i++)
{
for(int j=0;j<splitList[i].length+i; j++)
{
indexList.add(splitList[i].substring(0,j).toLowerCase());
}
}
Firestore.instance.collection('agentRequest').add({'city':name,'searchIndex':indexList});
}
My Textfield and where i'm calling my function for storing my data in firestore.
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.text,
validator: ValidateLocation,
controller: _location,
// onSaved: (String val) {
// Location = val;
// },
maxLines: 1,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.add_location,
color: Colors.grey[800],
),
labelText: 'Enter Your Address:'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.text,
validator: ValidateLocation,
controller: _city,
// onSaved: (String val) {
// Location = val;
// },
maxLines: 1,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.add_location,
color: Colors.grey[800],
),
labelText: 'Enter City Name:'),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.phone,
validator: validateMobile,
controller: _phoneController,
// onSaved: (String val) {
// PhoneNumber = val;
// },
maxLines: 1,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.dialer_sip,
color: Colors.grey[800],
),
labelText: 'Enter Number:'),
),
),
Container(
//padding: EdgeInsets.only(left: 50, right: 50, bottom: 10),
//color: Colors.grey[200],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
TextFormField(
keyboardType: TextInputType.multiline,
validator: ValidateDescp,
controller: _description,
// onSaved: (String val) {
// Description = val;
// },
maxLines: 4,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.description,
color: Colors.grey[800],
),
labelText: 'Your Introduction:'),
//textAlign: TextAlign,
),
Container(
margin: EdgeInsets.only(left: 10, top: 10),
width: 165,
child: FlatButton(
disabledColor: Colors.grey[800],
onPressed: () async {
_validate = true;
agent.Name = _firstName.text;
agent.phoneNumber=_phoneController.text;
agent.age= _age.text;
agent.description=_description.text;
agent.address = _location.text;
agent.city=_city.text;
agent.uid = user.uid;
if (_sendToServer()) {
AgentDatabase().ApplyForAgent(agent);
uploadData(_city.text);
/*_firestore.collection("agentRequest").document(
firebaseUser.uid).setData({
"displayName": _firstName.text,
"age": _age.text,
'phoneNumber': _phoneController.text,
"address": _location.text,
"description": _description.text,
});*/
Navigator.pop(context);
_validate = false;
return true;
}
Navigator.pop(context);
My agent database class.
class AgentDatabase{
final Firestore _firestore = Firestore.instance;
Future<String> ApplyForAgent(AgentUser user) async {
String retVal = 'error';
try {
await _firestore.collection('agentRequest').document(user.uid).setData({
'displayName': user.Name,
'Address': user.address,
'description': user.description,
'phoneNumber': user.phoneNumber,
'city':user.city,
'uid': user.uid,
'User Type': "user",
// 'accountCreated' : Timestamp.now(),
});
retVal='Success';
}
catch (e) {
print(e);
}
return retVal;
}