Skip to content

Wrong code generation #6810

@vdevc

Description

@vdevc

Can we access your project?

  • I give permission for members of the FlutterFlow team to access and test my project for the sole purpose of investigating this issue.

Current Behavior

If I create a page state variable of type custom Data Type the generated code does not use this variable uniformly over the generated code.

Expected Behavior

The variable should be treated the same way independently of any condition.

Steps to Reproduce

  1. Create new project
  2. Head up to Data Types tab and create a new custom data Type called MyClass
  3. Add the following fields to your data type
    a. name, string
    b. valid, boolean
Image
  1. Open the HomePage in your project and add a page state variable
    a. Field name: myClassInstance
    b. Type: Data Type
    c. Data Type: MyClass
Image

Now have a look at the generated code:

The model class, generated by FF, is like this one:

class HomePageModel extends FlutterFlowModel<HomePageWidget> {
  ///  Local state fields for this page.

  MyClassStruct? myClassInstance;
  void updateMyClassInstanceStruct(Function(MyClassStruct) updateFn) {
    updateFn(myClassInstance ??= MyClassStruct());
  }
}

As you can see myClassInstance is declared as nullable, which is correct.

  1. Back in HomePage add a Column and, within the column, add a TextField and a Switch.
  2. For the text field widget, go in Text Properties and set initial value to the name member of the page state variable, like this
Image
  1. For the Switch widget, go in Switch Properties and set Switch Initial Value to the valid member of the page state variable, like this
Image

Now back to the generated code: the initState() method has been updated like this:

class _HomePageWidgetState extends State<HomePageWidget> {
  late HomePageModel _model;

  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => HomePageModel());

    _model.textController ??=
        TextEditingController(text: _model.myClassInstance?.name);
    _model.textFieldFocusNode ??= FocusNode();

    _model.switchValue = _model.myClassInstance!.valid;
  }
}

Here is the problem: when creating TextEditingController the generated code consider correctly myClassInstance as nullable and uses the conditional access in in _model.myClassInstance?.name
But for switchValue this does not happen and the code is _model.myClassInstance!.valid

Reproducible from Blank

  • The steps to reproduce above start from a blank project.

Bug Report Code (Required)

IT4slc/qx89OxL1E7c+BcvlR/SIROVA6ap0kscp/bxIgGIDzPOw+ZPWlVBJocM+mT2xfekSmmkAd0NqNvvHXV+0EIkyBY6Zqz7hUcznzUTmRUZOBDKmBPX9DO+FjfxGB07SzhBJQXctsc0UjwGWyBumuazDbHZe2ImdISq/LZO4=

Visual documentation

Image

Environment

- FlutterFlow version: 
- Platform:
- Browser name and version:
- Operating system and version affected:

Additional Information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions