0

I have problem in code on flutter always rebuilding Widgets If I want click button for reciving data from sqlite and firebase firestroe happing rebuilding widgets but I don't want rebuilding widgets what is soultion ? I want to stop rebuilding widgets in class or page this is the code ....................................................

import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cup_challenge/databaseHelper1.dart';
import 'package:cup_challenge/designButton.dart';
import 'package:cup_challenge/item.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

String _text_q = "";

class Online1 extends StatefulWidget {
  final String typeCup;
  final int creaded;
  const Online1({Key key, @required this.typeCup, @required this.creaded})
      : super(key: key);

  @override
  _Online1State createState() => _Online1State();
}

class _Online1State extends State<Online1> {
  User _user = FirebaseAuth.instance.currentUser;
  DatabaseHelper1 helper;
  Future<Item> future1;
  int countDatabase;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    
     _listen();
  }

  void _listen() async {
    try {
      if (_user != null) {
        
      
        await FirebaseFirestore.instance
            .collection("game")
            .doc(widget.creaded.toString())
            .get()
            .then((event) async {
            int _number1 = await event.data()["numberQuestion"];
            bool _run = await event.data()["runQuestion"];
            if (_run) {
              helper = DatabaseHelper1();
              future1 = helper.getUser("table_1", _number1);
              final todo = await future1;
              _text_q = todo.question;
            }
          }
        );
      }
    } catch (e) {
      print("ok");
    }
  }

  @override
  Widget build(BuildContext context) {
   
    return Scaffold(
      body: Column(
        children: [
          Center(
            child: Container(
              width: 222,
              height: 222,
              color: Colors.blue,
              child: Center(
                child: Text(
                  _text_q,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 16.0,
                      fontWeight: FontWeight.bold,
                      decoration: TextDecoration.none),
                  textDirection: TextDirection.rtl,
                ),
              ),
            ),
          ),
          SizedBox(
            height: 55,
          ),
          DesignButton3(
              text: "click",
              textColor: Colors.white,
              backgroundColor: Colors.blue,
              onPressed: () async {
                     helper = DatabaseHelper1();
                    countDatabase = await helper.getCount("table_1");
                    int rand1 = Random().nextInt(countDatabase) + 1;
                  await FirebaseFirestore.instance
                      .collection("game")
                      .doc(widget.creaded.toString())
                      .update({
                    "numberQuestion": rand1,
                    "currentPlayer": "player2",
                    "runQuestion": true
                  });
               
              }),
        ],
      ),
    );
  }
}
Mohammed Ali
  • 37
  • 1
  • 6

0 Answers0