Based on an example kindly provided by Amity support
import 'package:amity_sdk/amity_sdk.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await AmityCoreClient.setup(
option: AmityCoreClientOption(
apiKey: "<<API_KEY>>",
httpEndpoint: AmityRegionalHttpEndpoint.EU), // or US or SG
sycInitialization: true);
print("successfully setup");
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Amity Social Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Amity User Feed Query Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
final amityPosts = <AmityPost>[];
late PagingController<AmityPost> _controller;
final ScrollController scrollcontroller = ScrollController();
@override
void initState() {
AmityCoreClient.login('<<USER_ID>>')
.displayName('<<USER_ID>>')
.submit()
.then((value) {
print("check login value ${value}");
});
super.initState();
}
void queryFeed() {
print("enter query feed");
_controller = PagingController(
pageFuture: (token) => AmitySocialClient.newFeedRepository()
.getUserFeed("<<USER_ID>>") // or community or global feed
.includeDeleted(false)
.getPagingData(token: token, limit: 20),
pageSize: 20,
)..addListener(
() {
if (_controller.error == null) {
//handle results, we suggest to clear the previous items
//and add with the latest _controller.loadedItems
amityPosts.clear();
amityPosts.addAll(_controller.loadedItems);
print("check amity post ${amityPosts}");
//update widgets
} else {
print("error getting amity feed");
//error on pagination controller
//update widgets
}
},
);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_controller.fetchNextPage();
});
scrollcontroller.addListener(loadMore);
}
void loadMore() {
_controller.fetchNextPage()
//optional
.then((value) {
//success
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'<Check amityPosts variable for your posts>',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
queryFeed();
},
tooltip: 'Query user feed',
child: const Icon(Icons.add),
),
);
}
}