feedback icon indicating copy to clipboard operation
feedback copied to clipboard

Reinitializing of Widgets when keyboard appears or disappears

Open gustav77 opened this issue 2 years ago • 7 comments

Version

2.3.0

Library

feedback

Flutter channel

stable

Flutter version

2.8.1

Platform

iOS

Details

I have an App with lots of forms. Whenever the keyboard appears or disappears, the enclosing widget is recreated.

I love your widget, but I can't use it because of this issue.

Steps to reproduce

You. can reproduce this behaviour with your example. Just change the Text('Hello world') with a Textfield and add a print statement in the constructor:

class _SecondaryScaffold extends StatelessWidget {
  _SecondaryScaffold({required this.isDirty}) {
    print('initialize');
  }
  final bool isDirty;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Scaffold 2'),
      ),
      body: const Center(
        child: TextField(),
      ),
    );
  }
}

I can reproduce this behaviour on iOS and web.

Output of flutter doctor -v

[✓] Flutter (Channel stable, 2.8.1, on macOS 11.6.1 20G224 darwin-x64, locale de-DE)
    • Flutter version 2.8.1 at /Users/guenterguckelsberger/Documents/projekte/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (vor 3 Wochen), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/guenterguckelsberger/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.63.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.32.0

[✓] Connected device (2 available)
    • iPhone (mobile) • 761e360667185ab6f4cab5d7e14e7e8f7dd4b487 • ios            • iOS 15.1 19B74
    • Chrome (web)    • chrome                                   • web-javascript • Google Chrome 96.0.4664.110
No issues found

gustav77 avatar Jan 09 '22 13:01 gustav77

Hey thanks for reporting this. I assume that this behavior doesn't show without the feedback library, right?

@caseycrogers Do you think your recent changes might have introduced this behavior?

ueman avatar Jan 09 '22 15:01 ueman

yes, of course without the feedback library this behavior doesn't show up.

Am So., 9. Jan. 2022 um 16:37 Uhr schrieb Jonas Uekötter < @.***>:

Hey thanks for reporting this. I assume that this behavior doesn't show without the feedback library, right?

@caseycrogers https://github.com/caseycrogers Do you think your recent changes might have introduced this behavior?

— Reply to this email directly, view it on GitHub https://github.com/ueman/feedback/issues/164#issuecomment-1008320564, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASSYWZCGW5VIMLDYXXPBKDUVGTULANCNFSM5LR3XQLA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

-- Viele Grüße Günter Guckelsberger

MapleTec GmbH Schaffeld 8 21629 Neu Wulmstorf Germany

Tel.: +49 (172) 4026944

e-mail: @.*** @.***>

Web: https:// http://www.mapletec.de/m http://guckelsberger.wordpress.comapletec.blog

gustav77 avatar Jan 09 '22 16:01 gustav77

Playing around with this now. There's a decent chance my recent changes introduced this-hard to say without a repro I can test before and after the changes.

@gustav77 can you post a full repro? I cannot get initialize to print more than once from the following code and opening and closing the keyboard:

import 'package:flutter/material.dart';


void main() {
  runApp(BetterFeedback(child: _SecondaryScaffold(isDirty: false)));
}

class _SecondaryScaffold extends StatelessWidget {
  _SecondaryScaffold({required this.isDirty}) {
    print('\n\ninitialize\n\n');
  }
  final bool isDirty;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Scaffold 2'),
      ),
      body: const Center(
        child: TextField(),
      ),
    );
  }
}

I'd like to reproduce what you're describing before fully coming to a conclusion as to whether or not there's a bug with feedback that needs fixing, but in general your app shouldn't rely on widget objects not being re-constructed.

If you want initialization logic for your widget, you have to make it a stateful widget and override initState. The flutter framework makes no guarantees that a StatelessWidget object will be long lived and won't be re-constructed. It only guarantees that State<T> objects are long-lived. If you're putting init logic in the constructor body your app is going to have bugs with or without Better Feedback.

caseycrogers avatar Jan 09 '22 16:01 caseycrogers

Ah, I see you were referring to the example app: https://github.com/ueman/feedback/blob/06d4c3b835029df0e60934be2d74b522c90e9d22/feedback/example/lib/main.dart#L211

Using this example I can in fact get initialize to print on keyboard open and close. I'll retest with code from before my changes, but I suspect it'll still be there. The form fields all maintain expected state so there's no malformed behavior in the example code. I think this is a bug specific to your app stemming from using body constructors instead of initState for initialization logic.

caseycrogers avatar Jan 09 '22 16:01 caseycrogers

Just confirmed that initialize prints on keyboard open and close before the scale and expanding sheet changes: https://github.com/ueman/feedback/commit/d7b74f7b627399312acf3a98951b8cca766056b5 .

IMO this is expected behavior not a bug.

caseycrogers avatar Jan 09 '22 17:01 caseycrogers

This is still happening

mikewolfd avatar Dec 28 '22 05:12 mikewolfd

@mikewolfd is your issue exactly the same as the reproduction provided above? If so, you cannot (and should not) count on BetterFeedback or any other first or third party flutter widgets only calling widget constructors once. If your app was doing this and working before BetterFeedback then that's just a lucky accident.

Flutter makes no guarantees that a widget object will not be initialized redundantly, it only guarantees that a stateful widget's state object will be initialized once. If you are putting initialization logic in your widget constructors and expecting it to run only once then that is a bug in your code. Try using stateful widgets and initState instead.

If I've misunderstood your problem, can you provide a repro here?

@ueman I'd recommend closing this issue to make it clear that this is a bug in developers' code and not an issue with BetterFeedback.

caseycrogers avatar Dec 28 '22 07:12 caseycrogers