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

Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe #376

Closed
BunnyBuddy opened this issue Jan 18, 2023 · 2 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@BunnyBuddy
Copy link

BunnyBuddy commented Jan 18, 2023

Describe the bug
I am using an html editor to write description for a product, now the thing is, it works fine when the html editor is empty (default initialisation) when I use it in my Add Product Page but when I navigate to the Edit product page and try to assign a value to html editor it throws an exception.

Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
#0 Element._debugCheckStateIsActiveForAncestorLookup. (package:flutter/src/widgets/framework.dart:4153:9)
#1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4167:6)
#2 Element.dependOnInheritedWidgetOfExactType (package:flutter/src/widgets/framework.dart:4182:12)
#3 Theme.of (package:flutter/src/material/theme.dart:107:53)
#4 _HtmlEditorWidgetMobileState.build. (package:html_editor_enhanced/src/widgets/html_editor_widget_mobile.dart:456:34)

To Reproduce

this is my onInit function for the html editor
(descriptionField is the controller for this html editor)
(descriptionResult is the variable that contains raw string, the data to be shown in this html editor)

var descriptionResult = "<p>daaaaaaaaaaaaaaaaaaaaaaaaaa</p>";

onInit: () async {
                              productController.descriptionField.value.insertHtml(productController.descriptionResult.value);
                              String tags = await productController.descriptionField.value.getText();
                          },

This is the complete code for the HTML Editor

HtmlEditor(
                        controller: productController.descriptionField.value,
                        htmlEditorOptions: const HtmlEditorOptions(
                          hint: 'Your text here...',
                          shouldEnsureVisible: true,
                          darkMode: false,
                          characterLimit: 3000,
                        ),
                        htmlToolbarOptions: HtmlToolbarOptions(
                          renderSeparatorWidget: false,
                          toolbarPosition: ToolbarPosition.aboveEditor,
                          toolbarType: ToolbarType.nativeExpandable,
                          renderBorder: true,
                          buttonBorderColor: Colors.black45,
                          gridViewHorizontalSpacing: 0,
                          gridViewVerticalSpacing: 6,
                          initiallyExpanded: false,
                          dropdownBoxDecoration: BoxDecoration(border: Border.all(color: Colors.black45)),
                          defaultToolbarButtons: [
                            const StyleButtons(),
                            const FontButtons(
                              bold: true,
                              italic: true,
                              underline: false,
                              strikethrough: false,
                              clearAll: false,
                              subscript: false,
                              superscript: false,
                            ),

                            const ParagraphButtons(
                              caseConverter: false,
                              decreaseIndent: false,
                              increaseIndent: false,
                              lineHeight: false,
                              textDirection: false,
                            ),
                            const ListButtons(listStyles: false),
                            const InsertButtons(
                              link: false,
                              audio: false,
                              hr: false,
                            ),
            
                          ],

                          onDropdownChanged: (DropdownType type, dynamic changed, Function(dynamic)? updateSelectedItem) {
                            return true;
                          },
                          mediaLinkInsertInterceptor: (String url, InsertFileType type) {

                            return true;
                          },
                          mediaUploadInterceptor: (PlatformFile file, InsertFileType type) async {
            
                            if (type == InsertFileType.image) {
                              String imgUrl = await productController.postDescriptionImage(context: context, filePath: file.path!);
                              productController.descriptionField.value.insertNetworkImage(imgUrl);
                            }
                            return false;
                          },
                        ),
                        otherOptions: OtherOptions(
                          height: 0.26.sh,
                        ),
                        callbacks: Callbacks(
                          onBeforeCommand: (String? currentHtml) {
                          },
                          onChangeContent: (String? changed) async {
                            productController.descriptionResult.value = changed!;
                          },
                          onChangeCodeview: (String? changed) {
                          },
                          onChangeSelection: (EditorSettings settings) {

                          },
                          onDialogShown: () {
                          },
                          onEnter: () {
                          },
                          onFocus: () {
                          },
                          onBlur: () async {
                          },
                          onBlurCodeview: () {
                          },
                          onInit: () async {
                              productController.descriptionField.value.insertHtml(productController.descriptionResult.value);
                              String tags = await productController.descriptionField.value.getText();
                          },
                          onImageLinkInsert: (String? url) {
                           
                          },
                          onImageUpload: (FileUpload file) async {
                          },
                          onImageUploadError: (FileUpload? file, String? base64Str, UploadError error) {
                          },
                          onKeyDown: (int? keyCode) {
                           
                          },
                          onKeyUp: (int? keyCode) {
                           
                          },
                          onMouseDown: () {

                          },
                          onMouseUp: () {

                          },
                          onNavigationRequestMobile: (String url) {

                            return NavigationActionPolicy.ALLOW;
                          },
                          onPaste: () {

                          },
                          onScroll: () {

                          },
                        ),
                      ),

Expected behavior

It does the job right but it slows down the App and it throws this exception

Screenshots

Screenshot 2023-01-18 at 11 59 25 AM

Screenshot 2023-01-18 at 12 12 24 PM

Device:
I am using a simulator and an iPhoneX for testing purposes

If you need the entire code or explanation I am all for it but I've been trying to resolve this issue for a while and I just can't figure this one out.

@BunnyBuddy BunnyBuddy added the bug Something isn't working label Jan 18, 2023
@BunnyBuddy
Copy link
Author

Still couldn't find any solution..

@BunnyBuddy
Copy link
Author

Okay, So after soooo many days I've tried to resolve it by commenting the part
// if ((Theme.of(context).brightness == Brightness.dark || // widget.htmlEditorOptions.darkMode == true) && // widget.htmlEditorOptions.darkMode != false) { // //todo fix for iOS (https://github.com/pichillilorenzo/flutter_inappwebview/issues/695) // var darkCSS = // '<link href=\"${(widget.htmlEditorOptions.filePath != null ? "file:///android_asset/flutter_assets/packages/html_editor_enhanced/assets/" : "") + "summernote-lite-dark.css"}\" rel=\"stylesheet\">'; // await controller.evaluateJavascript( // source: "\$('head').append('$darkCSS');"); // }
from the file
/html-editor-enhanced/lib/src/widgets/html_editor_widget_mobile.dart

for some reason it wasn't getting the context.
It's not a proper solution but kind of a temp solution IF YOU DON'T WANT TO USE THE LIGHT OR DARK THEME

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

No branches or pull requests

2 participants