Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently getting Looking up a deactivated widget's ancestor is unsafe while using TextEditingController #167

Open
becjit opened this issue Mar 18, 2024 · 7 comments
Labels
bug Something isn't working in triage

Comments

@becjit
Copy link

becjit commented Mar 18, 2024

Bug report

Describe the bug
Approach 1:
In a stateful page declare textediting controller and disposing it. Use the controller inside the modalsheet. Getting deactivated widget. Observation: whenever the keyboard is opening up the dispose is getting called

Approach 2: initializing the controller inside the modal sheet. Same error

Also approach 2 I am not sure where to dispose it if I want to use it in leadingBarWidget

The example mentioned #47 is not clear

Steps to reproduce

Steps to reproduce the behavior:
WoltModalSheetPage(
topBarTitle: Text(
title,
style: theme.textTheme.titleMedium,
),

  isTopBarLayerAlwaysVisible: true,
  child: BlocBuilder<TemperatureUnitCubit, TemperatureUnitType>(
    builder: (unitContext, unitState) {
      return BlocConsumer<TemperatureBloc, TemperatureState>(
        listener: (context, state) {
          if (state is TemperatureDataAdded) {
            Navigator.of(modalSheetContext).pop();
          } else if (state is TemperatureDataEdited) {
            Navigator.of(modalSheetContext).pop();
          }
        },
        builder: (temperatureContext, temperaturestate) {
          if (unitState == TemperatureUnitType.celsius) {
            temperatureValueNotifier = ValueNotifier(37.5);
          } else {
            temperatureValueNotifier = ValueNotifier(98.4);
          }

          return Padding(
            padding: spacer.x.sm,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Gap(16.h),
                  Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border.all(
                        color: Colors.grey.withOpacity(0.2),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(12.r),
                    ),
                    child: Padding(
                      padding: spacer.y.xxs,
                      child: Column(
                        children: [
                         
              
                          SizedBox(
                            width: double.maxFinite,
                            child: TextField(
                           
                                minLines: 3,
                                maxLines: null,
                                keyboardType: TextInputType.multiline,
                                controller: TextEditingController(),
                                decoration: InputDecoration(
                                    labelText: notesLabel,
                                
                                    hintText: hintOptionalText,
                                    helperText: '',
                                    helperStyle: theme.textTheme.bodySmall!.copyWith(
                                      fontStyle: FontStyle.italic,
                                    )
                              
                                    ),
                                style: theme.textTheme.bodyLarge!
                            
                                ),
                          ),
                    
                          
                     
                         
                        ],
                      ),
                    ),
                  ),
                  Gap(height * .24.h),
                  Gap(36.h),
                ],
              ),
            ),
            //),
          );
        },
      );
    },
  ),
);

Expected behavior

The dispose should not get called. Also there should be a clear example of how to use the same texteditingcontroller in leadingtopbar widget and page and the correct way to dispose it


Additional context

Add any other context about the problem here.


@becjit becjit added bug Something isn't working in triage labels Mar 18, 2024
@ulusoyca
Copy link
Collaborator

Would this help?
#47

@becjit
Copy link
Author

becjit commented Mar 18, 2024

ijitmazumder@Abhijits-MacBook-Pro user_data_repository % flutter doctor -v
[✓] Flutter (Channel stable, 3.16.9, on macOS 13.4.1 22F770820d darwin-arm64, locale en-IN)
• Flutter version 3.16.9 on channel stable at /Users/abhijitmazumder/development/tooling/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 41456452f2 (8 weeks ago), 2024-01-25 10:06:23 -0800
• Engine revision f40e976bed
• Dart version 3.2.6

wolt_modal_sheet: ^0.4.1

@ulusoyca I took a look that. Could not really wrap my head around. How to inject the text editing controller

this is my decorator

decorator: (child) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => EnableStateButtonCubit(),
),
BlocProvider.value(value: BlocProvider.of(pageContext)),
BlocProvider.value(value: BlocProvider.of(pageContext)),
],
child: child,
);
},
pageListBuilder: (modalSheetContext) {
return [
getModalSheetTemperature(
modalSheetContext,

        //notesTextController: notesTextController,
      ),
    ];
  },

@becjit
Copy link
Author

becjit commented Mar 18, 2024

This is the example

I have referred to. Could not really wrap my head around.
To be honest, I could not find anything wrong with my code. So not sure why it is happening

As a side note I am heavily using the package and though little complex it is a wonderful package. I want to understand the root cause for this behaviour as it is not really obvious.

I spent the whole day by multiple combinations but nothing worked the moment keyboard is up the dispose is getting called

@becjit
Copy link
Author

becjit commented Mar 18, 2024

ok my last roll if dice was to copy paste

to check if I am doing anything silly but still the same. Do let me know if you want me to upload a video. Happy to help in anyway

@becjit
Copy link
Author

becjit commented Mar 26, 2024

Hi,
Is there any updates? Are you able to reproduce? Do let me know if you need any help

@becjit
Copy link
Author

becjit commented Jun 15, 2024

Hi just checkin in here. is anyone else facing similar problem?

@exkson
Copy link

exkson commented Aug 29, 2024

Hello @becjit
I'm facing the almost the same issue using this example from docs and replacing SelectionScreen by a StatefulWidget and adding a TextField wigdet with a TextEditingController. Each time I try to type text in field, I get Looking up a deactivated widget's ancestor is unsafe.

import 'package:flutter/material.dart';

class SelectionScreen extends StatefulWidget {
  const SelectionScreen({super.key});

  @override
  State<SelectionScreen> createState() => _SelectionScreenState();
}

class _SelectionScreenState extends State<SelectionScreen> {
  final _nameController = TextEditingController();

  @override
  void dispose() {
    _nameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pick an option'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              decoration: InputDecoration(labelText: "Enter some text"),
              controller: _nameController,
            ),
            Padding(
              padding: const EdgeInsets.all(8),
              child: ElevatedButton(
                onPressed: () {
                  // Close the screen and return "Yep!" as the result.
                  Navigator.pop(context, 'Yep!');
                },
                child: const Text('Yep!'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8),
              child: ElevatedButton(
                onPressed: () {
                  // Close the screen and return "Nope." as the result.
                  Navigator.pop(context, 'Nope.');
                },
                child: const Text('Nope.'),
              ),
            )
          ],
        ),
      ),
    );
  }
}

Can anyone help to reproduce please ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in triage
Projects
None yet
Development

No branches or pull requests

3 participants