Flutter Desktop app platform thread in the Firebase Firestore plugin error

134 views Asked by At

i am building desktop application with flutter. to load all the records from firebase database i am using stream builder but when i start the app it doesn't load any data from firestore database and gives an exception. when i navigate to homescreen (where stream builder is used) from some other screen then it works properly sometimes. this is the exception it is showing:

See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information. [ERROR:flutter/shell/common/shell.cc(1015)] The 'plugins.flutter.io/firebase_firestore/query/e908a231-bed2-4607-bf64-d5c76972c0dd' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.

this is my homescreen.dart code:

        import 'package:admin/productdetails.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    import 'package:google_fonts/google_fonts.dart';
    
    class homescreen extends StatefulWidget {
      static const String home = './homescreen';
      const homescreen({super.key});
    
      @override
      State<homescreen> createState() => _homescreenState();
    }
    
    class _homescreenState extends State<homescreen> {
      @override
      Widget build(BuildContext context) {
        double screenWidth = MediaQuery.of(context).size.width;
        double screenHeight = MediaQuery.of(context).size.height;
        Color customColor = HexColor("#fd7b2e");
        return Scaffold(
          body: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/home.png'),
                fit: BoxFit.cover,
              ),
            ),
            child: Stack(
              children: [
                Container(
                  alignment: Alignment.topCenter,
                  width: double.infinity,
                  child: Text(
                    'ADMIN',
                    style: GoogleFonts.getFont(
                      'Oswald',
                      textStyle: TextStyle(
                          fontSize: screenWidth * 0.04,
                          fontWeight: FontWeight.bold,
                          color: Colors.white),
                    ),
                  ),
                ),
                Stack(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(
                          top: screenWidth * 0.1,
                          left: screenWidth * 0.01,
                          ),
                      child: Container(
                        height: screenWidth * 0.4,
                        width: screenWidth * 0.29,
                        decoration: const BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                          color: Colors.white70,
                        ),
                        child: Column(
                          children: [
                            Builder(
                              builder: (BuildContext context) {
                                return SingleChildScrollView(
                                  child: Container(
                                    child: Column(
                                      children: [
                                        Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.center,
                                          children: [
                                            Text(
                                              'New Orders',
                                              style: GoogleFonts.getFont(
                                                'Outfit',
                                                textStyle: TextStyle(
                                                  fontSize: screenWidth * 0.02,
                                                  fontWeight: FontWeight.bold,
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                        SizedBox(
                                          height: screenHeight * 0.5,
                                          child: StreamBuilder<QuerySnapshot>(
                                            stream: FirebaseFirestore.instance
                                                .collection('orders')
                                                .snapshots(),
                                            builder: (BuildContext context,
                                                AsyncSnapshot<QuerySnapshot>
                                                    snapshot) {
                                              if (snapshot.hasError) {
                                                return Text(
                                                    'Error: ${snapshot.error}');
                                              }
    
                                              if (snapshot.connectionState ==
                                                  ConnectionState.waiting) {
                                                return const CircularProgressIndicator();
                                              }
    
                                              final List<DocumentSnapshot>
                                                  orderDocs = snapshot.data!.docs;
    
                                              if (orderDocs.isEmpty) {
                                                return const Text(
                                                    'no orders yet');
                                              }
    
                                              return ListView.builder(
                                                itemCount: orderDocs.length,
                                                itemBuilder: (BuildContext context,
                                                    int index) {
                                                  final orderData =
                                                      orderDocs[index].data()
                                                          as Map<String, dynamic>;
                                                  final List<dynamic> imageUrl =
                                                      orderData['imageUrls'];
                                                  final pname = orderData['pname'];
                                                  final cname = orderData['cname'];
    
                                                  return GestureDetector(
                                                    onTap: () {
                                                      Navigator.push(
                                                        context,
                                                        MaterialPageRoute(
                                                          builder: (context) => productdetails(
                                                            pname: pname,
                                                            // Add more data variables as needed
                                                          ),
                                                        ),
                                                      );
                                                    },
                                                    child: SizedBox(
                                                      height: screenHeight * 0.16,
                                                      child: Card(
                                                        color: customColor,
                                                        child: Stack(
                                                          children: [
                                                            Stack(
                                                              children: [
                                                                Row(
                                                                  children: [
                                                                    Padding(
                                                                      padding: EdgeInsets.all(
                                                                          screenWidth *
                                                                              0.01),
                                                                      child:
                                                                          CircleAvatar(
                                                                        radius:
                                                                            screenWidth *
                                                                                0.03,
                                                                        backgroundImage:
                                                                            NetworkImage(
                                                                                imageUrl.first),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Padding(
                                                                  padding: EdgeInsets.only(
                                                                      left:
                                                                          screenWidth *
                                                                              0.08,
                                                                      top: screenHeight *
                                                                          0.026),
                                                                  child: Column(
                                                                    children: <Widget>[
                                                                      Row(
                                                                        children: [
                                                                          Expanded(
                                                                            child:
                                                                                Text(
                                                                              'Product: $pname sdfsdafdasfdasfdsafsdafdsfsdafdsfdsfsadfasdfasdf',
                                                                              style:
                                                                                  GoogleFonts.getFont(
                                                                                'Outfit',
                                                                                textStyle:
                                                                                    TextStyle(
                                                                                  fontSize: screenWidth * 0.015,
                                                                                  color: Colors.white,
                                                                                ),
                                                                              ),
                                                                              softWrap:
                                                                                  false,
                                                                              maxLines:
                                                                                  1,
                                                                              overflow:
                                                                                  TextOverflow.ellipsis,
                                                                            ),
                                                                          ),
                                                                        ],
                                                                      ),
                                                                      Row(
                                                                        children: [
                                                                          Expanded(
                                                                            child:
                                                                                Text(
                                                                              'By Company: $cname',
                                                                              style:
                                                                                  GoogleFonts.getFont(
                                                                                'Outfit',
                                                                                textStyle:
                                                                                    TextStyle(
                                                                                  fontSize: screenWidth * 0.01,
                                                                                  color: Colors.white,
                                                                                ),
                                                                              ),
                                                                              softWrap:
                                                                                  false,
                                                                              maxLines:
                                                                                  1,
                                                                              overflow:
                                                                                  TextOverflow.ellipsis,
                                                                            ),
                                                                          ),
                                                                        ],
                                                                      ),
                                                                    ],
                                                                  ),
                                                                ),
                                                              ],
                                                            ),
                                                          ],
                                                        ),
                                                      ),
                                                    ),
                                                  );
                                                },
                                              );
                                            },
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
    
              ],
            ),
          ),
        );
      }
    }
    
    class HexColor extends Color {
      HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
      static int _getColorFromHex(String hexColor) {
        hexColor = hexColor.toUpperCase().replaceAll("#", "");
        if (hexColor.length == 6) {
          hexColor = "FF$hexColor";
        }
       

 return int.parse(hexColor, radix: 16);
  }
}

this is my main.dart code:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'firebase_options.dart';
import 'homescreen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
      options: DefaultFirebaseOptions.web
  );
  runApp(const Eaziprep());
}

class Eaziprep extends StatelessWidget {
  const Eaziprep({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Eazi Prep',
      home: const homescreen(),
      routes: {
        homescreen.home: (context) => const homescreen(),
      },
    );
  }
}
0

There are 0 answers