-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Gen 2][Bugfix] Update the flutter mobile support doc. #7245
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -524,7 +524,7 @@ Running this command will scaffold a lightweight Amplify project in your current | |||||
Amplify gen2 provides a new way to develop applications. Now you are able to run your application with a sandbox environment and generate the configuration files for your application. To run your application with a sandbox environment, you can run the following command: | ||||||
|
||||||
```bash | ||||||
npx amplify sandbox --config-format=dart --config-out-dir=lib | ||||||
npx amplify sandbox --config-format dart --config-out-dir lib | ||||||
``` | ||||||
|
||||||
### Adding Authentication | ||||||
|
@@ -631,31 +631,33 @@ After the Amplify creation process, you can see a resource.ts file in the amplif | |||||
The default code will create a Todo model with content and isDone property. The authorization rules below specify that owners, authenticated via your Auth resource can "create", "read", "update", and "delete" their own records. | ||||||
|
||||||
```typescript | ||||||
import { type ClientSchema, a, defineData } from '@aws-amplify/backend'; | ||||||
import { type ClientSchema, a, defineData } from "@aws-amplify/backend"; | ||||||
|
||||||
const schema = a.schema({ | ||||||
Todo: a | ||||||
.model({ | ||||||
content: a.string(), | ||||||
isDone: a.boolean() | ||||||
isDone: a.boolean(), | ||||||
createdAt: a.datetime(), | ||||||
updatedAt: a.datetime(), | ||||||
}) | ||||||
.authorization([a.allow.owner()]) | ||||||
.authorization([a.allow.owner()]), | ||||||
}); | ||||||
|
||||||
export type Schema = ClientSchema<typeof schema>; | ||||||
|
||||||
export const data = defineData({ | ||||||
schema, | ||||||
authorizationModes: { | ||||||
defaultAuthorizationMode: 'userPool' | ||||||
} | ||||||
defaultAuthorizationMode: "userPool", | ||||||
}, | ||||||
}); | ||||||
``` | ||||||
|
||||||
To generate the model classes out of GraphQL schema, you can run the following command: | ||||||
|
||||||
```bash | ||||||
npx amplify generate graphql-client-code --format=modelgen --model-target=dart --out=lib/models | ||||||
npx amplify generate graphql-client-code --format modelgen --model-target dart --out lib/models | ||||||
``` | ||||||
|
||||||
This will generate dart models under lib/models folder. | ||||||
|
@@ -720,6 +722,7 @@ class _TodoScreenState extends State<TodoScreen> { | |||||
} else { | ||||||
safePrint('Creating Todo successful.'); | ||||||
} | ||||||
_refreshTodos(); | ||||||
}, | ||||||
), | ||||||
body: const Placeholder(), | ||||||
|
@@ -730,9 +733,31 @@ class _TodoScreenState extends State<TodoScreen> { | |||||
|
||||||
This will create a random Todo every time a user clicks on the floating action button. You can see the `ModelMutations.create` method is used to create a new Todo. | ||||||
|
||||||
And update the `Text('TODO Application')` line in your **main.dart** file to use the `TodoScreen` widget. | ||||||
And update the `MyApp` widget in your **main.dart** file like the following: | ||||||
|
||||||
```dart | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all file names are updated. |
||||||
class MyApp extends StatelessWidget { | ||||||
const MyApp({super.key}); | ||||||
@override | ||||||
Widget build(BuildContext context) { | ||||||
return Authenticator( | ||||||
child: MaterialApp( | ||||||
builder: Authenticator.builder(), | ||||||
home: const Scaffold( | ||||||
body: Column( | ||||||
children: [ | ||||||
SignOutButton(), | ||||||
Expanded(child: TodoScreen()), | ||||||
Equartey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
], | ||||||
), | ||||||
), | ||||||
), | ||||||
); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
Next add a `_todos` list to add the results from the API and call the refresh function: | ||||||
Next add a `_todos` list in `_TodoScreenState` to add the results from the API and call the refresh function: | ||||||
|
||||||
```dart | ||||||
List<Todo> _todos = []; | ||||||
|
@@ -744,6 +769,29 @@ void initState() { | |||||
} | ||||||
``` | ||||||
|
||||||
|
||||||
and create a new function called `_refreshTodos`: | ||||||
|
||||||
```dart | ||||||
Future<void> _refreshTodos() async { | ||||||
try { | ||||||
final request = ModelQueries.list(Todo.classType); | ||||||
final response = await Amplify.API.query(request: request).response; | ||||||
|
||||||
final todos = response.data?.items; | ||||||
if (response.hasErrors) { | ||||||
safePrint('errors: ${response.errors}'); | ||||||
return; | ||||||
} | ||||||
setState(() { | ||||||
_todos = todos!.whereType<Todo>().toList(); | ||||||
}); | ||||||
} on ApiException catch (e) { | ||||||
safePrint('Query failed: $e'); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
and update the body with the following code: | ||||||
|
||||||
```dart | ||||||
|
@@ -773,28 +821,6 @@ body: _todos.isEmpty == true | |||||
), | ||||||
``` | ||||||
|
||||||
and create a new function called `_refreshTodos`: | ||||||
|
||||||
```dart | ||||||
Future<void> _refreshTodos() async { | ||||||
try { | ||||||
final request = ModelQueries.list(Todo.classType); | ||||||
final response = await Amplify.API.query(request: request).response; | ||||||
|
||||||
final todos = response.data?.items; | ||||||
if (response.hasErrors) { | ||||||
safePrint('errors: ${response.errors}'); | ||||||
return; | ||||||
} | ||||||
setState(() { | ||||||
_todos = todos!.whereType<Todo>().toList(); | ||||||
}); | ||||||
} on ApiException catch (e) { | ||||||
safePrint('Query failed: $e'); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
Now let's add a update and delete functionality. | ||||||
|
||||||
For update, add the following code to the `onChanged` method of the `CheckboxListTile.adaptive` widget: | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these shouldn't be needed. They are autopopulated by Amplify Gen 2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so too. However, It did not auto-populated for me and Elijah. Is it a bug then?