Skip to content

Commit

Permalink
Fix errors with empty open food facts searches
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonp2412 committed Jul 12, 2024
1 parent c7bfd40 commit 7c0cf5e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/search_open_food_facts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
late final debouncedSearch = debounce(search);
final searchController = TextEditingController();

List<Product>? products;
List<Product> products = [];
bool searching = false;
bool cards = true;

Expand All @@ -35,9 +35,11 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
}

Future<void> search(String term) async {
if (term.isEmpty) return;
setState(() {
searching = true;
});

try {
final packageInfo = await PackageInfo.fromPlatform();
OpenFoodAPIConfiguration.userAgent = UserAgent(
Expand All @@ -54,7 +56,7 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
),
);
setState(() {
products = search.products;
products = search.products ?? [];
});
} finally {
setState(() {
Expand Down Expand Up @@ -91,7 +93,7 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
onPressed: () {
searchController.text = '';
setState(() {
products = null;
products = [];
searching = false;
});
},
Expand Down Expand Up @@ -130,7 +132,7 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator(),
);
if (products?.isEmpty == true)
if (products.isEmpty == true)
return const ListTile(title: Text("No products found"));

if (cards)
Expand All @@ -140,7 +142,7 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
alignment: WrapAlignment.center,
spacing: 8.0,
runSpacing: 8.0,
children: products!.map((product) {
children: products.map((product) {
final kj = product.nutriments?.getComputedKJ(
PerSize.oneHundredGrams,
);
Expand All @@ -152,9 +154,9 @@ class _SearchOpenFoodFactsState extends State<SearchOpenFoodFacts> {
);
return Expanded(
child: ListView.builder(
itemCount: products!.length,
itemCount: products.length,
itemBuilder: (context, index) {
final product = products![index];
final product = products[index];
final kj = product.nutriments?.getComputedKJ(
PerSize.oneHundredGrams,
);
Expand Down

0 comments on commit 7c0cf5e

Please sign in to comment.