aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.33k stars 248 forks source link

unable to retrieve data from a relationship in amplify with flutter #2083

Closed ahmad-olu closed 2 years ago

ahmad-olu commented 2 years ago

Description

I am trying to retrieve data from a 1 to 1 relationship on amplify but its not giving me the data i tired with @hasOne, @belongsTo and i even tried @hasOne with an argument which is @hasOne(fields: ["id"]) the same result

type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  username: String!
  email: AWSEmail!
  fullName: String
}
type Post @model @auth(rules: [{allow: public}]) {
  id: ID!
  title: String!
  content: String!
  dateCreated: AWSDateTime
  author: User @hasOne
}
type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  username: String!
  email: AWSEmail!
  fullName: String
}
type Post @model @auth(rules: [{allow: public}]) {
  id: ID!
  title: String!
  content: String!
  dateCreated: AWSDateTime
  author: User @hasOne(fields: ["id"])
}
type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  username: String!
  email: AWSEmail!
  fullName: String
  post: Post @belongsTo
}
type Post @model @auth(rules: [{allow: public}]) {
  id: ID!
  title: String!
  content: String!
  dateCreated: AWSDateTime
  author: User @hasOne
}

none of it game me the required result i also want to note that when i first save the data the data it returns the data from the relations but when i hot restart and try saving new values the new values are returned but the old relations are not

Categories

Steps to Reproduce

  1. created my schema on the aws amplify studio and pulled it into my app
  2. created my functions
    
    Future<void> saveUserAndPost() async {
    try {
      final user =
          User(username: 'blackblablabla', email: 'blackbla@g.com', id: '12345');
      final post = Post(
          title: 'This is the First title', content: 'seriously r this', author: user,);
      await Amplify.DataStore.save(user);
      await Amplify.DataStore.save(post);
      setState(() {});
    } on AuthException catch (e) {
      log(e.message);
    }
    }

void observeQuery() { _stream = Amplify.DataStore.observeQuery( Post.classType, sortBy: [Post.DATECREATED.descending()], ).listen((event) { setState(() { _posts = event.items; log( _posts .map((e) => '${e.author?.username} ===> ${e.author?.email} ====> ${e.title} =====> ${e.content}') .toString(), ); }); }); }

and the result i got below

User {id=12345, username=blackblablabla, email=blackbla@g.com, dateCreated=null, website=null, fullName=null, location=null, bio=null, profileImageUrl=null, following=null, followers=null, createdAt=null, updatedAt=null} (blackblablabla ===> blackbla@g.com ====> This is the second title =====> what is happening, blackblablabla ===> blackbla@g.com ====> This is the second title =====> what is happening, null ===> null ====> This is the First title =====> seriously r this, null ===> null ====> This is the First title =====> seriously r this)


as you can see the second title has the username and email but the first one does not. but the first one had a username and email before i hot restart.
4. its like it saved temporarily until i hot restart

### Screenshots

_No response_

### Platforms

- [ ] iOS
- [X] Android
- [ ] Web
- [ ] macOS
- [ ] Windows
- [ ] Linux

### Android Device/Emulator API Level

API 21

### Environment

```bash
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.0.5, on Microsoft Windows [Version 10.0.19043.1706], locale en-US)
Checking Android licenses is taking an unexpectedly long time...[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.1.0)
[√] Android Studio (version 2021.1)
[√] VS Code (version 1.70.2)
[√] Connected device (3 available)
[√] HTTP Host Availability

• No issues found!

Dependencies

Dart SDK 2.17.6
Flutter SDK 3.0.5
likning 1.0.0+1

dependencies:
- amplify_auth_cognito 0.6.5 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_core aws_common collection flutter meta plugin_platform_interface]
- amplify_datastore 0.6.5 [flutter amplify_datastore_plugin_interface amplify_core plugin_platform_interface meta collection async]
- amplify_flutter 0.6.5 [amplify_core amplify_datastore_plugin_interface amplify_flutter_android amplify_flutter_ios aws_common collection flutter meta plugin_platform_interface]
- amplify_storage_s3 0.6.5 [amplify_storage_s3_android amplify_storage_s3_ios amplify_core aws_common flutter meta plugin_platform_interface]
- anim_search_bar 2.0.2 [flutter]
- animated_text_kit 4.2.2 [flutter characters]
- auto_route 4.2.1 [flutter path collection meta universal_html]
- bloc 8.0.3 [meta]
- bloc_concurrency 0.2.0 [bloc stream_transform]
- cached_network_image 3.2.1 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web]
- dartz 0.10.1
- device_preview 1.1.0 [flutter flutter_localizations provider device_frame freezed_annotation json_annotation shared_preferences collection]
- division 0.9.0 [flutter]
- dough 1.0.3 [equatable flutter provider sensors vector_math]
- emoji_picker_flutter 1.3.0 [flutter shared_preferences]
- expandable 5.0.1 [flutter]
- file_picker 4.6.1 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface ffi path win32]
- flash 2.0.3+3 [flutter]
- flex_color_scheme 5.1.0 [flutter material_color_utilities]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_bloc 8.0.1 [flutter bloc provider]
- flutter_form_builder 7.5.0 [flutter intl collection]
- flutter_hooks 0.18.5+1 [flutter]
- flutter_image_compress 1.1.0 [flutter]
- flutter_local_notifications 9.7.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- flutter_lorem 2.0.0 [flutter]
- flutter_markdown 0.6.10+3 [flutter markdown meta path]
- flutter_quill 5.4.1 [flutter collection flutter_colorpicker flutter_keyboard_visibility image_picker photo_view quiver string_validator tuple url_launcher pedantic video_player characters youtube_player_flutter_quill diff_match_patch i18n_extension gallery_saver device_info_plus platform math_keyboard]
- flutter_staggered_grid_view 0.6.2 [flutter]
- formz 0.4.1
- freezed_annotation 2.1.0 [collection json_annotation meta]
- get_it 7.2.0 [async collection]
- google_fonts 3.0.1 [flutter http path_provider crypto]
- image_picker 0.8.5+3 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface]
- image_picker_for_web 2.1.8 [flutter flutter_web_plugins image_picker_platform_interface]
- injectable 1.5.3 [get_it]
- intl 0.17.0 [clock path]
- liquid_progress_indicator 0.4.0 [flutter]
- lottie 1.4.0 [archive flutter path vector_math]
- markdown_quill 1.0.0 [charcode collection flutter_quill markdown]
- material_floating_search_bar 0.3.7 [flutter meta]
- permission_handler 10.0.0 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface]
- pin_code_fields 7.4.0 [flutter]
- pinput 2.2.11 [flutter smart_auth]
- popover 0.2.6+3 [flutter]
- responsive_framework 0.2.0 [flutter collection]
- rxdart 0.27.5
- salomon_bottom_bar 3.3.1 [flutter]
- settings_ui 2.0.2 [flutter]
- share_plus 4.0.10 [meta mime flutter share_plus_platform_interface share_plus_linux share_plus_macos share_plus_windows share_plus_web]
- shimmer 2.0.0 [flutter]
- sizer 2.0.15 [universal_io flutter]
- tab_indicator_styler 2.0.0 [flutter]
- timeago 3.2.2
- timeline_tile 2.0.0 [flutter]
- translator 0.1.7 [http]
- upgrader 4.4.2 [flutter device_info_plus http os_detect package_info_plus shared_preferences url_launcher version xml html]
- uuid 3.0.6 [crypto]
- webview_flutter 3.0.4 [flutter webview_flutter_android webview_flutter_platform_interface webview_flutter_wkwebview]
- webview_flutter_plus 0.3.0+2 [flutter webview_flutter mime]

transitive dependencies:
- _fe_analyzer_shared 38.0.0 [meta]
- amplify_auth_cognito_android 0.6.5 [flutter]
- amplify_auth_cognito_ios 0.6.5 [amplify_core flutter]
- amplify_core 0.6.5 [aws_common collection flutter intl json_annotation meta plugin_platform_interface uuid]
- amplify_datastore_plugin_interface 0.6.5 [amplify_core collection flutter meta]
- amplify_flutter_android 0.6.5 [flutter]
- amplify_flutter_ios 0.6.5 [amplify_core flutter]
- amplify_storage_s3_android 0.6.5 [flutter]
- amplify_storage_s3_ios 0.6.5 [flutter]
- analyzer 3.4.1 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- archive 3.3.1 [crypto path]
- args 2.3.1
- async 2.8.2 [collection meta]
- aws_common 0.1.1 [async collection http meta stream_transform uuid]
- cached_network_image_platform_interface 1.0.0 [flutter flutter_cache_manager]
- cached_network_image_web 1.0.1 [flutter flutter_cache_manager cached_network_image_platform_interface]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- collection 1.16.0
- convert 3.0.2 [typed_data]
- cross_file 0.3.3+1 [js meta]
- crypto 3.0.2 [typed_data]
- csslib 0.17.2 [source_span]
- dbus 0.7.4 [args ffi meta xml]
- device_frame 1.1.0 [freezed_annotation flutter]
- device_info_plus 4.0.0 [flutter device_info_plus_platform_interface device_info_plus_macos device_info_plus_linux device_info_plus_web device_info_plus_windows]
- device_info_plus_linux 2.1.1 [device_info_plus_platform_interface file flutter meta]
- device_info_plus_macos 2.2.3 [device_info_plus_platform_interface flutter]
- device_info_plus_platform_interface 2.3.0+1 [flutter meta plugin_platform_interface]
- device_info_plus_web 2.1.0 [device_info_plus_platform_interface flutter_web_plugins flutter]
- device_info_plus_windows 2.1.1 [device_info_plus_platform_interface ffi flutter win32]
- diff_match_patch 0.4.1
- equatable 2.0.3 [collection meta]
- ffi 1.2.1
- file 6.1.2 [meta path]
- flutter_blurhash 0.7.0 [flutter]
- flutter_cache_manager 3.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid]
- flutter_colorpicker 1.0.3 [flutter]
- flutter_inappwebview_quill 5.4.6 [flutter]
- flutter_keyboard_visibility 5.3.0 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_web flutter]
- flutter_keyboard_visibility_platform_interface 2.0.0 [flutter meta plugin_platform_interface]
- flutter_keyboard_visibility_web 2.0.0 [flutter_keyboard_visibility_platform_interface flutter_web_plugins flutter]
- flutter_local_notifications_linux 0.5.0+1 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories]
- flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface]
- flutter_math_fork 0.6.3+1 [flutter flutter_svg provider meta collection tuple]
- flutter_plugin_android_lifecycle 2.0.7 [flutter]
- flutter_svg 1.1.4 [flutter meta path_drawing vector_math xml]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- gallery_saver 2.3.2 [flutter path_provider http path]
- gettext_parser 0.2.0
- glob 2.1.0 [async collection file path string_scanner]
- holding_gesture 1.1.0 [flutter]
- html 0.15.0 [csslib source_span]
- http 0.13.4 [async http_parser meta path]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- i18n_extension 5.0.0 [sprintf args analyzer gettext_parser equatable intl flutter]
- image_picker_android 0.8.5+1 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_ios 0.8.5+6 [flutter image_picker_platform_interface]
- image_picker_platform_interface 2.6.0 [cross_file flutter http plugin_platform_interface]
- js 0.6.4
- json_annotation 4.6.0 [meta]
- markdown 5.0.0 [args charcode meta]
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.4
- math_expressions 2.3.1 [vector_math]
- math_keyboard 0.1.7 [flutter flutter_localizations flutter_math_fork holding_gesture intl math_expressions petitparser]
- meta 1.7.0
- mime 1.0.2
- nested 1.0.0 [flutter]
- octo_image 1.0.2 [flutter flutter_blurhash]
- os_detect 2.0.1
- package_config 2.1.0 [path]
- package_info_plus 1.4.2 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web]
- package_info_plus_linux 1.0.5 [package_info_plus_platform_interface flutter path]
- package_info_plus_macos 1.3.0 [flutter]
- package_info_plus_platform_interface 1.0.2 [flutter meta plugin_platform_interface]
- package_info_plus_web 1.0.5 [flutter flutter_web_plugins http meta package_info_plus_platform_interface]
- package_info_plus_windows 1.0.5 [package_info_plus_platform_interface ffi flutter win32]
- path 1.8.1
- path_drawing 1.0.1 [vector_math meta path_parsing flutter]
- path_parsing 1.0.1 [vector_math meta]
- path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows]
- path_provider_android 2.0.17 [flutter path_provider_platform_interface]
- path_provider_ios 2.0.11 [flutter path_provider_platform_interface]
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_macos 2.0.6 [flutter path_provider_platform_interface]
- path_provider_platform_interface 2.0.4 [flutter platform plugin_platform_interface]
- path_provider_windows 2.0.7 [ffi flutter path path_provider_platform_interface win32]
- pedantic 1.11.1
- permission_handler_android 10.0.0 [flutter permission_handler_platform_interface]
- permission_handler_apple 9.0.4 [flutter permission_handler_platform_interface]
- permission_handler_platform_interface 3.7.0 [flutter meta plugin_platform_interface]
- permission_handler_windows 0.1.0 [flutter permission_handler_platform_interface]
- petitparser 5.0.0 [meta]
- photo_view 0.14.0 [flutter]
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- provider 6.0.3 [collection flutter nested]
- pub_semver 2.1.1 [collection meta]
- quiver 3.1.0 [matcher]
- sensors 2.0.3 [flutter]
- share_plus_linux 3.0.0 [share_plus_platform_interface file flutter meta url_launcher]
- share_plus_macos 3.0.1 [share_plus_platform_interface flutter]
- share_plus_platform_interface 3.0.3 [flutter meta mime plugin_platform_interface]
- share_plus_web 3.0.1 [share_plus_platform_interface url_launcher flutter flutter_web_plugins meta]
- share_plus_windows 3.0.1 [share_plus_platform_interface flutter meta url_launcher]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- shared_preferences_android 2.0.12 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.0.0 [flutter]
- shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- smart_auth 1.0.5 [flutter flutter_web_plugins]
- source_span 1.8.2 [collection path term_glyph]
- sprintf 6.0.0
- sqflite 2.0.3 [flutter sqflite_common path]
- sqflite_common 2.2.1+1 [synchronized path meta]
- stack_trace 1.10.0 [path]
- stream_transform 2.0.0
- string_scanner 1.1.0 [charcode source_span]
- string_validator 0.3.0
- synchronized 3.0.0+2
- term_glyph 1.2.0
- timezone 0.8.0 [path]
- tuple 2.0.0 [quiver]
- typed_data 1.3.1 [collection]
- universal_html 2.0.8 [async csslib charcode collection html meta source_span typed_data universal_io]
- universal_io 2.0.4 [collection crypto meta typed_data]
- url_launcher 6.1.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- url_launcher_android 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.1.0 [flutter plugin_platform_interface]
- url_launcher_web 2.0.12 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface]
- vector_math 2.1.2
- version 3.0.2
- video_player 2.4.5 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web]
- video_player_android 2.3.8 [flutter video_player_platform_interface]
- video_player_avfoundation 2.3.5 [flutter video_player_platform_interface]
- video_player_platform_interface 5.1.3 [flutter plugin_platform_interface]
- video_player_web 2.0.12 [flutter flutter_web_plugins video_player_platform_interface]
- watcher 1.0.1 [async path]
- webview_flutter_android 2.9.2 [flutter webview_flutter_platform_interface]
- webview_flutter_platform_interface 1.9.1 [flutter meta plugin_platform_interface]
- webview_flutter_wkwebview 2.9.2 [flutter path webview_flutter_platform_interface]
- win32 2.6.1 [ffi]
- xdg_directories 0.2.0+1 [meta path process]
- xml 6.1.0 [collection meta petitparser]
- yaml 3.1.1 [collection source_span string_scanner]
- youtube_player_flutter_quill 8.2.2 [flutter flutter_inappwebview_quill]

Device

samsung Ao2s

OS

Android 11

Deployment Method

Amplify CLI

CLI Version

9.2.1

Additional Context

No response

Amplify Config

{ "version": "1" }

HuiSF commented 2 years ago

Hi @ahmad-olu at this moment, when query, only @belongsTo nested model will be populated with data. Thank the 3rd of your example schema. When query User, the User.post field should be populated with data. For other types of relationship, currently separate queries are required to retrieve nested models.

We are investigating and improving developer experience on associated models operations, please feel free to follow up and to check the progress here: https://github.com/aws-amplify/amplify-flutter/issues/1449.

Can you verify/confirm if the @belongsTo return expected result for you?

dnys1 commented 2 years ago

Closing as this is expected given current model generation. Please leave a thumbs up and follow #1449, as @HuiSF suggested, for helping to prioritize better code generation surrounding @hasOne directives.