VeryGoodOpenSource / formz

A unified form representation in Dart used at Very Good Ventures 🦄

Home Page:https://pub.dev/packages/formz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot find private constructor for FormzInput.pure/FormzInput.dirty in newest Dart?

andreasmpet opened this issue · comments

I updated my to flutter version 2 today and started getting the following error message:

Couldn't find constructor 'FormzInput.'.
const FormzInput.dirty(T value) : this.
(value, false)

By removing the read only file protection of the cached package and re-naming the anonymous constructor, things start working again.

Is this a change in dart that you will have to take into account, or is this a bug?

Here's a minimum reproducible project.

Removing the "with TestMixin" fixes the error.

As I said, not sure if this is a bug in Dart or what. It didn't happen before updating to Dart 2.12

import 'package:flutter/material.dart';
import 'package:formz/formz.dart';

enum UserDetailsValidationError { empty, incorrectFormat, tooShort, termsNotAccepted }

mixin TestMixin {
  void testMethod() {}
}

class FocusedValue  {
  final String value;
  final bool hasFocus;

  const FocusedValue(this.value, this.hasFocus);

  FocusedValue copyWith({String value, bool hasFocus}) {
    return FocusedValue(value ?? this.value, hasFocus ?? this.hasFocus);
  }
}

class EmailInput extends FormzInput<FocusedValue, UserDetailsValidationError> with TestMixin {
  const EmailInput.pure(FocusedValue value) : super.pure(value);

  const EmailInput.dirty(FocusedValue value) : super.dirty(value);

  @override
  UserDetailsValidationError validator(FocusedValue value) {
    final trimmed = value?.value ?? "";
    if (trimmed.isEmpty) return UserDetailsValidationError.empty;

    if (!value.hasFocus) {
      final emailRegexp =
          r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$";
      final matches = RegExp(emailRegexp).hasMatch(trimmed);
      if (!matches) return UserDetailsValidationError.incorrectFormat;
    }

    return null;
  }

  EmailInput copyWith(FocusedValue value) {
    if (this.pure) {
      return EmailInput.pure(value);
    } else {
      return EmailInput.dirty(value);
    }
  }
}

void main() {
  // final f = TestSubclass.toast(SomeClass(true));
  EmailInput f = EmailInput.dirty(FocusedValue("value", true));
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

I changed my local pub cache version of formz.dart and got it working again by removing the const keywords from the named constructors:

 const FormzInput._(this.value, [this.pure = true]);

 /// Constructor which create a `pure` [FormzInput] with a given value.
 FormzInput.pure(T value) : this._(value);

 /// Constructor which create a `dirty` [FormzInput] with a given value.
 FormzInput.dirty(T value) : this._(value, false);

@felangel Could you verify that and release a fix if it works, please?

I'm not sure about the underlying reason, but maybe related to dart-lang/language#823?

@SebastianEngel the const constructor should not have anything to do with this and it seems more likely to be related to a pub-cache issue. I tried your reproduction sample and had no issues. I would recommend clearing your pub-cache and restarting your analysis server. Let me know if that helps 👍

Hi @felangel, I run pub dart cache repair and restarted the analysis server. Still the same problem, even after updating the formz dependency to 0.4.0.

Running Gradle task 'assembleDebug'...                          
../../../.pub-cache/hosted/pub.dartlang.org/formz-0.4.0/lib/formz.dart:96:36: Error: Couldn't find constructor 'FormzInput._'.
  const FormzInput.pure(T value) : this._(value);
                                   ^^^^
../../../.pub-cache/hosted/pub.dartlang.org/formz-0.4.0/lib/formz.dart:99:37: Error: Couldn't find constructor 'FormzInput._'.
  const FormzInput.dirty(T value) : this._(value, false);

This is my environment specification:

environment:
  sdk: ">=2.10.0 <3.0.0"
  flutter: 2.0.5

I intentionally left the min Dart SDK at 2.10 in order to avoid migrating to null safety right now.
Do you have an idea?

Just a fyi, you can upgrade dart without migrating to null safety! I'm running that exact setup :)

This bug is still a thing, and has nothing to do with pub cache. I forked my own version of this project and renamed the constructors to avoid having to wait.

:(
Same issue here

I have the same problem 😩😩

Some different solution to downgrade to no-null safety version? ://

Error: Couldn't find constructor 'FormzInput.'.
const FormzInput.pure(T value) : this.
(value);

I have created dart-lang/sdk#46581 to track this since it seems it's not related to the formz package.

I don't understand why @SebastianEngel solution was declined. His solution works perfectly, while all other suggestions fail

I don't understand why @SebastianEngel solution was declined. His solution works perfectly, while all other suggestions fail

This is a bug in Dart that was recently fixed. It has nothing to do with the formz package. See dart-lang/sdk#46581

Closing since this should be resolved 👍