flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
164.12k stars 27.05k forks source link

Failed assertion: line 5140 pos 12: '_history.isNotEmpty': is not true. #105494

Closed murtaza-bhaisaheb closed 2 years ago

murtaza-bhaisaheb commented 2 years ago
import 'package:flutter/material.dart';
import 'package:line_icons/line_icons.dart';
import 'package:tuza/constants/global_variables.dart';
import 'package:tuza/features/admin/screens/add_product_screen.dart';
import 'package:tuza/features/admin/services/admin_services.dart';
import 'package:tuza/models/product.dart';

class ProductsScreen extends StatefulWidget {
  const ProductsScreen({Key? key}) : super(key: key);

  @override
  State<ProductsScreen> createState() => _ProductsScreenState();
}

class _ProductsScreenState extends State<ProductsScreen> {
  final AdminServices adminServices = AdminServices();
  List<Product>? productList;

  @override
  void initState() {
    super.initState();
    fetchAllProducts();
  }

  fetchAllProducts() async {
    productList = await adminServices.fetchAllProducts(context);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return productList == null ? const CircularProgressIndicator() : Scaffold(
      body: const Center(child: Text('Products'),),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).pushNamed(AddProductScreen.routeName);
        },
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20.0),
        ),
        backgroundColor: GlobalVariables.backgroundColor,
        tooltip: "Add a product",
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(18.0),
            border: Border.all(color: GlobalVariables.selectedNavBarColor)
          ),
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Icon(
              LineIcons.plus, color: GlobalVariables.selectedNavBarColor,
            ),
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}
darshankawar commented 2 years ago

@murtaza-bhaisaheb The code sample you shared contains third party plugin and custom code, but in order to narrow down the issue to framework, please provide complete minimal reproducible code sample without any third party plugin that triggers the assertion you are reporting.

Thanks.

murtaza-bhaisaheb commented 2 years ago

// A function to fetch the list:

Future<List>? fetchAllProducts(BuildContext context) async { final UserProvider userProvider = Provider.of(context, listen: false); List productList = []; try { http.Response res = await http.get(Uri.parse('$uri/admin/get-products'), headers: { 'Content-Type': 'application/json; charset=UTF-8', 'x-auth-token': userProvider.user.token, });

  httpErrorHandle(
      response: res,
      context: context,
      onSuccess: () {
        for (int i = 0; i < jsonDecode(res.body).length; i++) {
          productList.add(
            Product.fromJson(
              jsonEncode(
                jsonDecode(res.body)[i],
              ),
            ),
          );
        }
        showSnackBar(context, 'Product added successively');
        Navigator.of(context).pop();
      });
} catch (e) {
  showSnackBar(context, e.toString());
}

return Future.value(productList);

}

// In the UI:

Future<List?>? fetchAllProducts() async { productList = await adminServices.fetchAllProducts(context); return Future.value(productList); // setState(() {}); }

// And in build method:

FutureBuilder( future: fetchAllProducts(), builder: (BuildContext context, AsyncSnapshot snapshot) { switch (snapshot.connectionState) { case ConnectionState.waiting: return const Text('Loading....'); default: if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return const Scaffold( body: Text('Loaded'), ); } }

murtaza-bhaisaheb commented 2 years ago

Loading appears for 1 second and then black screen appears

darshankawar commented 2 years ago

The updated code you shared still contains third party plugin provider, so I suggest you to ask this question on support channels like StackOverflow.

Closing from here for now.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.