Assistance required to get search query data render on screen. flutter/dart - firebase

I made this notes app which uses firestore to save data and it is working fine, no problem whatsoever.
Now I am trying to implement a search filter on notes and I can't find the right method to do it.
Here is my code that renders the notes on the screen
here is the reference image
List<Widget> _buildNotesView(
BuildContext context, NoteFilter filter, List<Note> notes) {
if (notes?.isNotEmpty != true) {
return [_buildBlankView(filter.noteState)];
}
final asGrid = filter.noteState == NoteState.deleted || notesView;
final factory = asGrid ? NotesGrid.create : NotesList.create;
final showPinned = filter.noteState == NoteState.unspecified;
if (!showPinned) {
return [
factory(notes: notes, onTap: _onNoteTap),
];
}
final partition = _partitionNotes(notes);
final hasPinned = partition.item1.isNotEmpty;
final hasUnpinned = partition.item2.isNotEmpty;
final _buildLabel = (String label, [double top = 26]) => SliverToBoxAdapter(
child: Container(
padding:
EdgeInsetsDirectional.only(start: 26, bottom: 25, top: top),
child: Text(
label,
style: TextStyle(
fontFamily: selectedFont,
color: kHintTextColorLight,
fontWeight: FontWeights.medium,
fontSize: 12,
),
),
),
);
//TODO
if (searchController.text.isNotEmpty) {
notes.forEach((note) {
if (note.title
.toLowerCase()
.contains(searchController.text.toLowerCase()) ||
note.content
.toLowerCase()
.contains(searchController.text.toLowerCase())) ;
//Do something here?
});
}
return [
if (hasPinned) _buildLabel('PINNED', 0),
if (hasPinned) factory(notes: partition.item1, onTap: _onNoteTap),
if (hasPinned && hasUnpinned) _buildLabel('OTHERS'),
factory(notes: partition.item2, onTap: _onNoteTap),
];
}
Any help would be great. I'm open to learning.

Related

getx and weird problem: text won't change on UI

I am trying to create a search function from firebase
and i am using getx for the state management
I counter this weird problem, when I try to use the function directly it work, but when I use the firebase function it does not.
I will explain more on the code snipit.
this is my search textForm
TextFormField(
onChanged: (value) => database.searchForId(value),
decoration: InputDecoration(
focusColor: Colors.lightGreenAccent,
prefixIcon: Icon(
Icons.search,
color: Colors.lightGreenAccent,
),
labelText: 'Search...',
labelStyle: TextStyle(color: Colors.lightGreenAccent),
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: Colors.lightGreenAccent),
),
)),
as you can see I am using on change value
database code for search
searchForId(searchText) {
FirebaseFirestore.instance
.collection('inventory')
.where('id', isGreaterThanOrEqualTo: searchText)
.get()
.then((snapshot) {
var data = snapshot.docs[0];
if (data['id'] != searchText) {
return;
} else {
searchController.changeId(data['id']);
searchController.changeName(data['name']);
searchController.changeQuantity(data['quantity']);
searchController.changeCost(data['cost']);
searchController.changeMyPrice(data['myPrice']);
}
});
}
and the controller
class SearchController extends GetxController {
var id = 'No info'.obs;
var name = 'No info'.obs;
var quantity = 'No info'.obs;
var cost = 'No info'.obs;
var myPrice = 'No info'.obs;
changeId(_id) {
id(_id);
print(id);
}
the print show me that the value is changed, but never update it on my UI
Obx(() => Text(searchController.id.value))
so i trid to call the controller dirctly (not using database file) and it work just fine, and UI updated
any idea why ?
the weird is the print show me the function get called and working fine ( i mean when i use database file as well ) but it never update the UI
I think u forgot to keep
SearchController searchcontroller = Get.put(SearchController());
if you initialized it before just find that controller using
SearchController searchcontroller = Get.find();
var id = 'No info'.obs;
searchController.changeId(data['id']);
changeId(_id) {
id(_id);
print(id);
}
In order to update your text in UI, you can either update your code
changeId(_id) {
id(_id);
print(id);
update(); // Calling update manually. update() is a function in GetxController to trigger the observable change.
}
or
changeId(String _id) {
id.value = _id;
}

Flutter: How to remove awaits to take advantage of Firebase Offline Persistence?

I'm using a drop-down list (DropDown), whose elements are obtained from Firebase. The form works right, however when the internet connection is lost the Firebase Offline Persistence property doesn't work and the CircularProgressIndicator stays active. Reading some responses such as Using Offline Persistence in Firestore in a Flutter App, it is indicated that awaits should not be handled, however it is not clear to me how to achieve it:
class EstanqueAlimentarPage extends StatefulWidget {
#override
_EstanqueAlimentarPageState createState() => _EstanqueAlimentarPageState();
}
class _EstanqueAlimentarPageState extends State<EstanqueAlimentarPage> {
final formKey = GlobalKey<FormState>();
AlimentoBloc alimentoBloc = new AlimentoBloc();
AlimentoModel _alimento = new AlimentoModel();
AlimentarModel alimentar = new AlimentarModel();
List<AlimentoModel> _alimentoList;
bool _alimentoDisponible = true;
#override
void dispose() {
alimentoBloc.dispose();
super.dispose();
}
#override
void initState() {
_obtenerListaAlimentoUnaVez();
super.initState();
}
Future<void> _obtenerListaAlimentoUnaVez() async {
_alimentoList = await alimentoBloc.cargarAlimento(idEmpresa); // Await that I want to eliminate
if (_alimentoList.length > 0) { // Here appears a BAD STATE error when the internet connection goes from off to on
_alimento = _alimentoList[0];
_alimentoDisponible = true;
} else {
_alimentoDisponible = false;
}
_cargando = false;
setState(() {});
}
#override
Widget build(BuildContext context) {
return Form(
key: formKey,
child: Column(
children: <Widget> [
_crearTipoAlimento(_alimentoList),
SizedBox(height: 8.0),
_crearComentarios(),
]
)
),
_crearBoton('Guardar'),
}
Widget _crearTipoAlimento(List<AlimentoModel> lista) {
return Container(
decoration: _cajaBlanca,
child:
!_cargando // If it isn't loading, Dropdown must be displayed
? DropdownButtonFormField<AlimentoModel>(
decoration: InputDecoration(
labelText: 'Nombre del Alimento',
contentPadding: EdgeInsets.only(top:5.0),
prefixIcon: Icon(FontAwesomeIcons.boxOpen, color: Theme.of(context).primaryColor,),
border: InputBorder.none,
),
value: _alimento,
items: lista.map((AlimentoModel value) {
return DropdownMenuItem<AlimentoModel>(
child: Text(value.nombre),
value: value,
);
}).toList(),
onChanged: (_alimentoDisponible) ? (AlimentoModel _alimentoSeleccionado) {
print(_alimentoSeleccionado.nombre);
_alimento = _alimentoSeleccionado;
setState(() {});
} : null,
disabledHint: Text('No hay Alimento en Bodega'),
onSaved: (value) {
alimentar.idAlimento = _alimento.idAlimento;
alimentar.nombreAlimento = _alimento.nombreRef;
}
)
: Center (child: CircularProgressIndicator(strokeWidth: 1.0,))
);
}
Widget _crearComentarios() {
return TextFormField(
// -- DESIGN OTHER FIELDS -- //
onSaved: (value) {
alimentar.comentarios = value;
}
),
);
}
Widget _crearBoton(String texto) {
return RaisedButton(
// -- DESIGN -- //
onPressed: (_guardando) ? null : _submit,
),
);
}
void _submit() {
// CODE TO WRITE FORM IN FIREBASE
}
}
The function code from my BLOC is:
Future<List<AlimentoModel>> cargarAlimento(String idEmpresa, [String filtro]) async {
final alimento = await _alimentoProvider.cargarAlimento(idEmpresa, filtro); //It's one await more
_alimentoController.sink.add(alimento);
return alimento;
}
And the Query from PROVIDER is:
Future<List<AlimentoModel>> cargarAlimento(String idEmpresa, [String filtro]) async {
Query resp;
final List<AlimentoModel> alimento = new List();
resp = db.child('empresas').child(idEmpresa).child('bodega/1').child('alimento')
.orderByChild('cantidad').startAt(0.000001);
return resp.once().then((snapshot) {
if (snapshot.value == null) return [];
if (snapshot.value['error'] != null) return [];
snapshot.value.forEach((id, alim){
final temp = AlimentoModel.fromJson(Map<String,dynamic>.from(alim));
temp.idAlimento = id;
alimento.add(temp);
});
return alimento;
});
When using Firebase offline, you omit the await only on things that change the server (e.g., creating or updating a record). So you won't wait for the server to say "yes I wrote it", you assume that it's written.
In your case, however, you are not writing data, you are reading data. You will have to keep await in your example. The way you load your data has orderByChild and startAt, maybe those are preventing offline loading. Normally, you get it if it's already in the cache: https://firebase.google.com/docs/firestore/manage-data/enable-offline#get_offline_data
You mention a BAD STATE error, maybe if you provide that, we may be able to pinpoint the issue a bit better.

How to convert Future<int> to int?

So I'm trying to display a pie chart using the fl_chart plugin. The data for the chart is being retrieved from firestore. I have this function that is used to display the data:
List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) {
return List.generate(length, (i) {
final isTouched = i == touchedIndex;
final double fontSize = isTouched ? 25 : 16;
final double radius = isTouched ? 60 : 50;
return PieChartSectionData(
color: Color(int.parse(cerealData[i].colorVal)),
value: cerealData[i].rating,
title: cerealData[i].rating.toString(),
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
});
}
The List.generate() takes an int as an argument. Since I'm displaying realtime data, I'm trying to get the number of documents present in my collection. For that, I have a function called getLength():
void getLength(AsyncSnapshot<QuerySnapshot> snapshot) async {
length = await Firestore.instance.collection('cereal').snapshots().length;
cerealData =
snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
}
However, when I run the code, I get:
Another exception was thrown: type 'Future<int>' is not a subtype of type 'int'
The entire code:
class _FlChartPageState extends State<FlChartPage> {
int touchedIndex;
var length;
List<Cereal> cerealData;
void getLength(AsyncSnapshot<QuerySnapshot> snapshot) async {
length = await Firestore.instance.collection('cereal').snapshots().length;
cerealData =
snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('cereal').snapshots(),
builder: (context, snapshot) {
getLength(snapshot);
if (!snapshot.hasData)
return CircularProgressIndicator();
else {
return PieChart(PieChartData(
pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) {
setState(() {
if (pieTouchResponse.touchInput is FlLongPressEnd ||
pieTouchResponse.touchInput is FlPanEnd) {
touchedIndex = -1;
} else {
touchedIndex = pieTouchResponse.touchedSectionIndex;
}
});
}),
borderData: FlBorderData(show: false),
sectionsSpace: 0,
centerSpaceRadius: 40,
sections: showSection(snapshot)));
}
}),
);
}
List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) {
return List.generate(length, (i) {
final isTouched = i == touchedIndex;
final double fontSize = isTouched ? 25 : 16;
final double radius = isTouched ? 60 : 50;
return PieChartSectionData(
color: Color(int.parse(cerealData[i].colorVal)),
value: cerealData[i].rating,
title: cerealData[i].rating.toString(),
radius: radius,
titleStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.bold,
color: const Color(0xffffffff)),
);
});
}
}
I read somewhere that awaiting the future gets rid of the Future. But that doesn't work here.
How do I fix this?
Edit: It works if I simply pass the number of documents instead of length in List.generate(). But this won't work if there are changes to the collection. So how do I convert Future to int?
I think you aren't getting the length of the documents, you are getting the length of the snapshots if you want to get the documents length :
QuerySnapshot querySnapshot = await Firestore.instance.collection('cereal').getDocuments();
int length = querySnapshot.documents.length;
In get getLength function you are trying to get length which is actually async task which returns future and because of that you are getting following error.
Change your method with following metod
getLength()async{
Firestore.instance.collection('cereal').snapshots().length.then((len){
length = len;
cerealData =
snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
});
}

Using Form With PageView Overwrites Previous Form Data With Each Page Flutter

I am using a pageview to display a form and get data. But, with every page, the previous data is is overwritten. I'm not sure why. Here is what I'm doing:
I want to get these pieces of information from each page:
final Map<String, dynamic> ratingCapsule = {
"prodId": null,
"starRating": null,
"_foodRating": null,
"_drinkRating": null,
"recommend": null
};
Page View:
Expanded(
flex: 2,
child: Form(
key: _userRatingKey,
child: IndexedStack(
index: index,
children: <Widget>[
PageView.builder(
itemBuilder: (context, i) {
return _prodctReview2(context, i);
},
itemCount: currentReview.order.orderDetails.length,
controller: _pageController,
)
],
),
)),
Validator:
void _submit(srating, effectRating, usageRating, userRecommend) {
final FormState form = _userRatingKey.currentState;
if (form.validate()) {
ratingCapsule['starRating'] = srating;
ratingCapsule['_foodRating'] = effectRating;
ratingCapsule['_drinkRating'] = _drinkRating;
ratingCapsule['recommend'] = userRecommend;
form.save();
print(ratingCapsule['prodId']);
print(ratingCapsule['starRating']);
print(ratingCapsule['_foodrating']);
print(ratingCapsule['_drinkRating']);
print(ratingCapsule['recommend']);
print(ratingCapsule.values);
ratingsList.add(ratingCapsule);
print(ratingsList);
setState(() {
vaildForm = true;
print(ratingCapsule);
});
_clearInput();
ratingCapsule.clear();
nextPage();
} else {
print('Invalid Form');
print(ratingCapsule.values);
print(ratingCapsule);
}
}
On the first page I get:
[{prodId: 51, starRating: 4.0, _foodRating: 5, _drinkRating: 1, recommend: 1}]
But on the second page I get:
[{prodId: 55, starRating: 5.0, _foodRating: 5, _drinkRating: 1, recommend: 1}, {prodId: 55, starRating: 5.0, _foodRating: 5, _drinkRating: 1, recommend: 1}]
Overwriting the previous data with the new information. How do I stop this?
You can use TextEdittingController for each field.

Cannot recognize Text when using Firebase_ml_vision kit from camera

Im trying to build an app that uses the Text Recognition that comes with the Firebase_ml_vision package. When i use the Firebase.VisionImage.fromFile(pickedImage) and i download a picture from the internet with text it works perfectly, but when i try to use an image taken with my iPhone Xr camera it does not read the text decently. The output is similar to this:
flutter: 2000
AADAT4A
flutter: 2000
AADAT4A
flutter: 20000
flutter: 03012
flutter: 2000
this happens when i try to print blocks.text lines or elements.
Does anyone have an ideia how to implement this?
I used this code: (its not all the code but the necessary)
File pickedImage;
List<String> words = [];
Future pickImage() async {
var tempStore =
await ImagePicker.pickImage(source: ImageSource.gallery); //gallery here
setState(() {
pickedImage = tempStore;
isImageLoaded = true;
});
}
Future pickCameraImage() async {
var tempStore =
await ImagePicker.pickImage(source: ImageSource.camera); //camera here
setState(() {
pickedImage = tempStore;
isImageLoaded = true;
});
}
Future readText() async {
FirebaseVisionImage ourImage = FirebaseVisionImage.fromFile(pickedImage);
TextRecognizer recognizeText = FirebaseVision.instance.textRecognizer();
VisionText readText = await recognizeText.processImage(ourImage);
for (TextBlock block in readText.blocks) {
// final Rect boundingBox = block.boundingBox;
// final List<Offset> cornerPoints = block.cornerPoints;
// final String text = block.text;
// final List<RecognizedLanguage> languages = block.recognizedLanguages;
for (TextLine line in block.lines) {
print(block.text);
for (TextElement word in line.elements) {
words.add(word.text);
}
}
} isTextLoaded = true;
}
In the Ui part:
(...)
RaisedButton(
child: Text('Pick an Image'),
onPressed: pickImage,
),
SizedBox(
height: 10.0,
),
RaisedButton(
child: Text('Pick a Camera Image'),
onPressed: pickCameraImage,
),
SizedBox(
height: 10.0,
),
RaisedButton(
child: Text('Read Text'),
onPressed: readText,
),
(...)
I do not have any errors. Anyone with more experience can help me? Thank you

Resources