jifalops / simple_autocomplete_formfield

A Flutter widget that wraps a TextFormField and assists with autocomplete functionality.
MIT License
42 stars 16 forks source link

type 'String' is not a subtype of type '<My_Class_Type>' #3

Closed Kydarun closed 6 years ago

Kydarun commented 6 years ago

Coding that went wrong:

Container(
  margin: EdgeInsets.only(top: 5.0),
  child: SimpleAutocompleteFormField<Country>(
    itemBuilder: (context, item) {
      return Container(
        padding: EdgeInsets.all(10.0),
        decoration: BoxDecoration(
          border: Border.all(color: Theme.of(context).primaryColor, width: 1.0),
        ),
        child: Row(
          children: <Widget>[
            SvgPicture.string(
              item.svg,
              width: 30.0,
              height: 30.0,
            ),
            Container(
              margin: EdgeInsets.only(left: 5.0),
              child: Text(
                item.name
              ),
            )
          ],
        ),
      );
    },
    itemParser: ItemParser(
      itemToString: (item) => item.name,
      itemFromString: (value) => null,
    ),
    onSearch: (search) async => _countries(search),
    containerBuilder: (context, items) =>
      LimitedBox(
        maxHeight: 150.0,
        child: Card(
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: items,
            ),
          ),
        ),
      ),
    decoration: InputDecoration(
        labelText: 'Country',
        border: OutlineInputBorder(),
        contentPadding: EdgeInsets.all(12.0)
    ),
    onSaved: (value) {
      _formData.country = value.name;
    },
    initialValue: user?.country ?? '',
  ),
),

Stacktrace:

I/flutter (14738): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (14738): The following assertion was thrown building EditProfile(dirty, state: EditProfileState#f4664):
I/flutter (14738): type 'String' is not a subtype of type 'Country'
I/flutter (14738):
I/flutter (14738): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (14738): more information in this error message to help you determine and fix the underlying cause.
I/flutter (14738): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (14738):   https://github.com/flutter/flutter/issues/new
I/flutter (14738):
I/flutter (14738): When the exception was thrown, this was the stack:
I/flutter (14738): #0      EditProfileState.build (package:fgt_membership/components/edit_profile.dart)
I/flutter (14738): #1      StatefulElement.build (package:flutter/src/widgets/framework.dart:3730:27)
I/flutter (14738): #2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3642:15)
I/flutter (14738): #3      Element.rebuild (package:flutter/src/widgets/framework.dart:3495:5)
I/flutter (14738): #4      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2242:33)
I/flutter (14738): #5      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:626:20)
I/flutter (14738): #6      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter (14738): #7      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter (14738): #8      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter (14738): #9      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter (14738): #10     _invoke (dart:ui/hooks.dart:120:13)
I/flutter (14738): #11     _drawFrame (dart:ui/hooks.dart:109:3)
I/flutter (14738): ════════════════════════════════════════════════════════════════════════════════════════════════════

My Country class:

@JsonSerializable()
class Country extends Object with _$CountrySerializerMixin {
  String name;
  String svg;

  Country({this.name, this.svg});

  factory Country.fromJson(Map<String, dynamic> json) => _$CountryFromJson(json);
}

And here is my async function _countries(search):

Future<List<Country>> _countries(search) async{
  try {
    if (search.isEmpty) {
      return List<Country>();
    }
    final countriesResponse = await Api().get('countries', {
      'query': search
    });
    return (countriesResponse as List).map((json) => Country.fromJson(json)).toList();
  }
  catch (e, stacktrace) {
    _scaffoldState.currentState?.showSnackBar(SnackBar(
        content: Text(
            e.message
        ),
        duration: Duration(seconds: 2)
    ));
    print('$stacktrace');
    return List<Country>();
  }
}

The onSearch function never executed. The intended API response (which I tested at Postman & is working fine):

[
    {
        "name": "Malaysia",
        "svg": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"2800\" height=\"1400\">\n<rect width=\"2800\" height=\"1400\" id=\"red\" style=\"fill:#cc0001\"/>\n<rect width=\"2800\" height=\"100\" y=\"1300\" id=\"white\" style=\"fill:#fff\"/>\n<use transform=\"translate(0,-200)\" id=\"use3035\" xlink:href=\"#white\"/>\n<use transform=\"translate(0,-400)\" id=\"use3038\" xlink:href=\"#white\"/>\n<use transform=\"translate(0,-600)\" id=\"use3062\" xlink:href=\"#white\"/>\n<use transform=\"translate(0,-800)\" id=\"use3064\" xlink:href=\"#white\"/>\n<use transform=\"translate(0,-1000)\" id=\"use3066\" xlink:href=\"#white\"/>\n<use transform=\"translate(0,-1200)\" id=\"use3068\" xlink:href=\"#white\"/>\n<rect width=\"1400\" height=\"800\" id=\"blue\" style=\"fill:#010066\"/>\n<path d=\"M 576,100 C 409.85356,100 275,234.40553 275,400 C 275,565.59446 409.85356,699.99999 576,700 C 636.02725,700 691.95466,682.43566 738.92693,652.21675 C 711.57383,661.65623 682.2175,666.81914 651.65599,666.81914 C 504.3291,666.81915 384.75944,547.64674 384.75944,400.80929 C 384.75944,253.97184 504.32908,134.79944 651.65599,134.79944 C 684.21403,134.79944 715.40185,140.61419 744.2578,151.26671 C 696.21698,118.90999 638.30493,100 576,100 z\" id=\"path4945\" style=\"fill:#fc0\"/>\n<path d=\"M 914.28574,471.42858 L 814.74817,418.17806 L 844.17832,527.16068 L 777.60261,435.99589 L 756.83247,546.9551 L 736.40474,435.93234 L 669.5481,526.89131 L 699.31432,418.00001 L 599.61295,470.94319 L 673.67755,385.75059 L 560.87853,390.19195 L 664.57212,345.5715 L 561.01668,300.63137 L 673.80146,305.42069 L 600.00003,220.00001 L 699.53759,273.25053 L 670.10745,164.26791 L 736.68316,255.4327 L 757.4533,144.47349 L 777.88103,255.49624 L 844.73767,164.53728 L 814.97145,273.42858 L 914.67282,220.4854 L 840.60822,305.67799 L 953.40723,301.23664 L 849.71365,345.85709 L 953.26909,390.79722 L 840.48431,386.0079 z\" transform=\"matrix(1.2737922,0,0,1.2422928,-89.4427,-29.478379)\" id=\"path4959\" style=\"fill:#fc0\"/>\n</svg>\n"
    }
]

Flutter doctor:

[✓] Flutter (Channel beta, v0.5.1, on Mac OS X 10.13.6 17G65, locale en-MY)
  • Flutter version 0.5.1 at /Users/ken/flutter
  • Framework revision c7ea3ca377 (3 months ago), 2018-05-29 21:07:33 +0200
  • Engine revision 1ed25ca7b7
  • Dart version 2.0.0-dev.58.0.flutter-f981f09760

[✓] Android toolchain - develop for Android devices (Android SDK 28.0.2)
  • Android SDK at /Users/ken/Library/Android/sdk
  • Android NDK location not configured (optional; useful for native profiling support)
  • Platform android-28, build-tools 28.0.2
  • ANDROID_HOME = /Users/ken/Library/Android/sdk
  • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
  • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
  • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
  • Xcode at /Applications/Xcode.app/Contents/Developer
  • Xcode 9.4.1, Build version 9F2000
  • ios-deploy 1.9.2
  • CocoaPods version 1.5.3

[✓] Android Studio (version 3.1)
  • Android Studio at /Applications/Android Studio.app/Contents
  • Flutter plugin version 26.0.1
  • Dart plugin version 173.4700
  • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[!] VS Code (version 1.26.1)
  • VS Code at /Applications/Visual Studio Code.app/Contents
  • Flutter extension not installed; install from
    https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected devices (1 available)
  • Pixel 2 • HT7AK1A04533 • android-arm64 • Android 9 (API 28)

Flutter extension is already installed in my Visual Studio Code, however due to this bug, it's safe to ignore this warning.

jifalops commented 6 years ago

You are setting a string for the initialValue, but it requires a Country. If you don't want there to be an initial country just omit the initialValue argument altogether.

Also if _formData.country is a Country and not a string, your onSaved argument has an error.

Kydarun commented 6 years ago

Ah, sorry, my bad for missing out. Thank you for the reply. I will close this issue now.