androidflutterdartflutter-dependenciesdart-pub

Error in flutter: Exception caught by rendering library, Each child must be laid out exactly once


this is my GameScreen.dart code:

import 'package:flutter/material.dart;
import "dart:math";

import 'package:guess_my_number/welcome_text.dart';

class GameScreen extends StatefulWidget {
  final TextEditingController userNumber;
  const GameScreen({super.key, required this.userNumber});

  @override
  State<GameScreen> createState() => _GameScreenState();
}

class _GameScreenState extends State<GameScreen> {
  String? displayNumber;
  int? opponentsGuess;
  int? correctNumber;
  int totalTries = 1;
  int min = 0;
  int max = 100;
  List<String> tries = [];
  final Random _random = Random();



 @override
  void initState() {
    super.initState();
    widget.userNumber.addListener(_updateDisplayNumber);
    displayNumber = widget.userNumber.text;
    correctNumber = int.tryParse(widget.userNumber.text);
    _updateMinMax();
    if (correctNumber != null) {
      setState(() {
        min = correctNumber! - 100;
        max = correctNumber! + 100;
        opponentsGuess = _generateRandomNumber();
        _checkGuess();
      });
    }
  }

  @override
  void dispose() {
    widget.userNumber.removeListener(_updateDisplayNumber);
    super.dispose();
  }

  void _updateDisplayNumber() {
    setState(() {
      displayNumber = widget.userNumber.text;
      correctNumber = int.tryParse(displayNumber ?? "0");
    });
  }

  void _updateMinMax() {
    setState(() {
      if (correctNumber != null) {
        min = 0;
        max = 100;
      }
    });
  }

  void incNumber() {
    if (correctNumber == null || opponentsGuess == null) return;
    if (opponentsGuess! < correctNumber!) {
      setState(() {
        min = opponentsGuess!;
        opponentsGuess = _generateRandomNumber();
        totalTries++;
      });
      _checkGuess();
    } else {
      _showAlert("Do not lie!", "Your guess is too high!");
    }
  }

  void decNumber() {
    if (correctNumber == null || opponentsGuess == null) return;
    if (opponentsGuess! > correctNumber!) {
      setState(() {
        max = opponentsGuess!;
        opponentsGuess = _generateRandomNumber();
        totalTries++;
      });
      _checkGuess();
    } else {
      _showAlert("Do not lie!", "Your guess is too low!");
    }
  }

  int _generateRandomNumber() {
    return _random.nextInt(max - min + 1) + min;
  }

  void _checkGuess() {
    if (opponentsGuess == correctNumber) {
      tries.add("The opponent tries times:#$totalTries");
      _showAlert("Success!",
          "The opponent guessed the correct number!, Total Tries=$totalTries");
    } else {
      _showAlert(
          "Guess updated", "The opponent's new guess is $opponentsGuess");
    }
  }

  void _showAlert(String title, String message) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          content: Text(message),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text("OK"),
            )
          ],
        );
      },
    );
  }



@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Game Screen'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            padding:
                const EdgeInsets.symmetric(vertical: 5.0, horizontal: 50.0),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.white, width: 5.0),
            ),
            child: const WelcomeText(
              message: "Opponent's Guess",
              color: Colors.white,
            ),
          ),
          const SizedBox(height: 20.0),
          Container(
            alignment: Alignment.center,
            padding:
                const EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),
            width: 350.0,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.orange, width: 5.0),
            ),
            child: WelcomeText(
              message: '$opponentsGuess',
              color: Colors.orange,
            ),
          ),
          const SizedBox(height: 20.0),
          Container(
            padding: const EdgeInsets.all(10.0),
            width: 350.0,
            height: 150.0,
            color: Colors.brown[900],
            child: Column(
              children: [
                Text(
                  "Higher or Lower",
                  style: TextStyle(
                    fontSize: 30.0,
                    color: Colors.amber[800],
                  ),
                ),
                const SizedBox(height: 20.0),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    SizedBox(
                      width: 160.0,
                      height: 50.0,
                      child: ElevatedButton(
                        onPressed: decNumber,
                        style: ElevatedButton.styleFrom(
                          foregroundColor: Colors.orange[300],
                        ),
                        child: const Text(
                          "-",
                          style: TextStyle(fontSize: 20.0),
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 160.0,
                      height: 50.0,
                      child: ElevatedButton(
                        onPressed: incNumber,
                        style: ElevatedButton.styleFrom(
                          foregroundColor: Colors.orange[300],
                        ),
                        child: const Text(
                          "+",
                          style: TextStyle(fontSize: 20.0),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          const SizedBox(height: 20.0),
          Expanded(
            child: ListView.builder(
              itemCount: tries.length,
              itemBuilder: (context, index) {
                return Container(
                  key: ValueKey(tries[index]), // Unique key for each item
                  padding: const EdgeInsets.symmetric(
                      vertical: 4.0, horizontal: 2.0),
                  decoration: BoxDecoration(
                    color: Colors.green[800],
                    borderRadius: BorderRadius.circular(4.0),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.grey.withOpacity(0.5),
                        blurRadius: 4.0,
                        spreadRadius: 4.0,
                        offset: const Offset(2, 2),
                      ),
                    ],
                  ),
                  child: ListTile(
                    contentPadding: const EdgeInsets.all(0.3),
                    title: Text(
                      tries[index],
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 14.0,
                        fontWeight: FontWeight.normal,
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

this is my GradientContainer.dart:

import 'package:flutter/material.dart';
import 'package:guess_my_number/game_screen.dart';
import 'package:guess_my_number/home_screen.dart';

class GradientContainer extends StatefulWidget {
  const GradientContainer({
    required this.firstColor,
    required this.secondColor,
    super.key,
  });

  final Color firstColor;
  final Color secondColor;

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

class _GradientContainerState extends State<GradientContainer> {
  final TextEditingController _userNumber = TextEditingController();
  @override
  void dispose() {
    _userNumber.dispose();
    super.dispose();
  }

  String setActivePage = 'HomeScreen';

  void screenUpdate() {
    setState(() {
      setActivePage =
          setActivePage == 'HomeScreen' ? 'GameScreen' : 'HomeScreen';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            widget.firstColor,
            widget.secondColor,
          ],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
      ),
      child: setActivePage != 'HomeScreen'
          ? GameScreen(
              userNumber: _userNumber,
            )
          : Home_Screen(
              onScreenUpdate: screenUpdate,
              userNumber: _userNumber, // Pass the function reference
            ),
    );
  }
}

this is my main.dart:

import "package:flutter/material.dart";
import "package:guess_my_number/gradient_container.dart";

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: GradientContainer(
          firstColor: Color.fromARGB(255, 255, 215, 0),
          secondColor: Color.fromARGB(255, 255, 236, 179),
        ),
      ),
    ),
  );
}

when i am trying to run it i am getting this error and screen shows nothing

android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@b92fda8 fN = 20 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/IMM_LC  (15388): showSoftInput(View,I)
I/IMM_LC  (15388): ssi() - flag : 0 view : com.example.guess_my_number reason = SHOW_SOFT_INPUT
I/IMM_LC  (15388): ssi() view is not EditText
D/InsetsController(15388): show(ime(), fromIme=true)
I/IMM_LC  (15388): showSoftInput(View,I)
I/IMM_LC  (15388): ssi() - flag : 0 view : com.example.guess_my_number reason = SHOW_SOFT_INPUT
I/IMM_LC  (15388): ssi() view is not EditText
D/InsetsController(15388): show(ime(), fromIme=true)
I/ViewRootImpl@78173e7[MainActivity](15388): ViewPostIme pointer 0
I/ViewRootImpl@78173e7[MainActivity](15388): ViewPostIme pointer 1

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Container(bg: BoxDecoration(gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Color(0xffffd700), Color(0xffffecb3)], tileMode: TileMode.clamp)), constraints: BoxConstraints(biggest)):
dependOnInheritedWidgetOfExactType<_LocalizationsScope>() or dependOnInheritedElement() was called before _GameScreenState.initState() completed.
When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.

The relevant error-causing widget was:
    Container Container:file:///D:/Flutter-Project/guess_my_number/lib/gradient_container.dart:38:12

When the exception was thrown, this was the stack:
#0      StatefulElement.dependOnInheritedElement.<anonymous closure> (package:flutter/src/widgets/framework.dart:5720:9)
#1      StatefulElement.dependOnInheritedElement (package:flutter/src/widgets/framework.dart:5763:6)
#2      Element.dependOnInheritedWidgetOfExactType (package:flutter/src/widgets/framework.dart:4779:14)
#3      Localizations.of (package:flutter/src/widgets/localizations.dart:529:48)
#4      debugCheckHasMaterialLocalizations.<anonymous closure> (package:flutter/src/material/debug.dart:92:23)
#5      debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:113:4)
#6      showDialog (package:flutter/src/material/dialog.dart:1426:10)
#7      _GameScreenState._showAlert (package:guess_my_number/game_screen.dart:106:5)
#8      _GameScreenState._checkGuess (package:guess_my_number/game_screen.dart:100:7)
#9      _GameScreenState.initState.<anonymous closure> (package:guess_my_number/game_screen.dart:35:9)
#10     State.setState (package:flutter/src/widgets/framework.dart:1203:30)
#11     _GameScreenState.initState (package:guess_my_number/game_screen.dart:31:7)
#12     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5618:55)
#13     ComponentElement.mount (package:flutter/src/widgets/framework.dart:5463:5)
#14     Element.inflateWidget (package:flutter/src/widgets/framework.dart:4340:16)
#15     Element.updateChild (package:flutter/src/widgets/framework.dart:3843:20)
#16     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#17     Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#18     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#19     Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#20     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#21     Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#22     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#23     Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#24     StatelessElement.update (package:flutter/src/widgets/framework.dart:5563:5)
#25     Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#26     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#27     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#28     Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#29     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2905:19)
#30     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1136:21)
#31     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:443:5)
#32     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1392:15)
#33     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1313:9)
#34     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1171:5)
#35     _invoke (dart:ui/hooks.dart:312:13)
#36     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:419:5)
#37     _drawFrame (dart:ui/hooks.dart:283:31)

════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
Each child must be laid out exactly once.
The relevant error-causing widget was:
════════════════════════════════════════════════════════════════════════════════
D/InputMethodManager(15388): startInputInner - Id : 0
I/InputMethodManager(15388): startInputInner - mService.startInputOrWindowGainedFocus
D/InsetsController(15388): show(ime(), fromIme=true)
I/IMM_LC  (15388): hsifw() - flags=0, caller=android.view.inputmethod.InputMethodManager.hideSoftInputFromWindow:1846 android.view.inputmethod.InputMethodManager.hideSoftInputFromWindow:1815 io.flutter.plugin.editing.TextInputPlugin.hideTextInput:401 io.flutter.plugin.editing.TextInputPlugin.access$300:39 io.flutter.plugin.editing.TextInputPlugin$1.hide:101
I/IMM_LC  (15388): hideSoftInputFromWindow - mService.hideSoftInput
D/InsetsSourceConsumer(15388): setRequestedVisible: visible=false, type=19, host=com.example.guess_my_number/com.example.guess_my_number.MainActivity, from=android.view.InsetsSourceConsumer.hide:253 android.view.ImeInsetsSourceConsumer.hide:68 android.view.ImeInsetsSourceConsumer.hide:74 android.view.InsetsController.hideDirectly:1473 android.view.InsetsController.controlAnimationUnchecked:1139 android.view.InsetsController.applyAnimation:1456 android.view.InsetsController.applyAnimation:1437 android.view.InsetsController.hide:1006 android.view.ViewRootImpl$ViewRootHandler.handleMessageImpl:6482 android.view.ViewRootImpl$ViewRootHandler.handleMessage:6403
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@7781c9f fN = 21 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
D/InsetsSourceConsumer(15388): ensureControlAlpha: for ITYPE_NAVIGATION_BAR on com.example.guess_my_number/com.example.guess_my_number.MainActivity
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@ff579b5 fN = 22 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@d98284a fN = 23 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@94be3bb fN = 24 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@25520d8 fN = 25 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@d50fc31 fN = 26 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@7387f16 fN = 27 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@acb0097 fN = 28 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ

════════ Exception caught by rendering library ═════════════════════════════════
Each child must be laid out exactly once.
The relevant error-causing widget was:
════════════════════════════════════════════════════════════════════════════════
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@e677284 fN = 29 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@d082a6d fN = 30 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@6b576a2 fN = 31 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@8b50f33 fN = 32 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@42b32f0 fN = 33 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@58a0069 fN = 34 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@2185aee fN = 35 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@2276b8f fN = 36 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ
I/IMM_LC  (15388): notifyImeHidden
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: t = android.view.SurfaceControl$Transaction@d288e1c fN = 37 android.view.SyncRtSurfaceTransactionApplier.applyTransaction:94 android.view.SyncRtSurfaceTransactionApplier.lambda$scheduleApply$0$SyncRtSurfaceTransactionApplier:71 android.view.SyncRtSurfaceTransactionApplier$$ExternalSyntheticLambda0.onFrameDraw:4
I/ViewRootImpl@78173e7[MainActivity](15388): mWNT: merge t to BBQ

════════ Exception caught by rendering library ═════════════════════════════════
Each child must be laid out exactly once.
The relevant error-causing widget was:
════════════════════════════════════════════════════════════════════════════════

I once tried to take entire widget inside SafeArea and later remove it set heigth with sizedbox inside Expanded, both did not work I am new as beginner in flutter and learning it, it is one of the project i am making, any idea what is causing the problem and how to fix it P.S: Thank you for answering


Solution

  • Well , I fixed the error, the error was happening because _showAlert was being called with _checkGuess() when the GameScreen was being render, so I had to use WidgetBinding.instance.addPostFrameCallBack to make sure the _showAlert within _checkGuess was called only after the return was fully render, this is the code i have done below:

    void _checkGuess() {
        tries.insert(0, "The opponent tries times: #$totalTries = $opponentsGuess");
        if (opponentsGuess == correctNumber) {
          // Use WidgetsBinding to delay the alert call
          WidgetsBinding.instance.addPostFrameCallback((_) {
            _showAlert(
              "Success!",
              "The opponent guessed the correct number! Total Tries = $totalTries",
              gameOver: true,
            );
          });
        } else {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            _showAlert(
              "Guess updated",
              "The opponent's new guess is $opponentsGuess",
            );
          });
        }
      }
    

    and it worked