On the previous page, I had a list of news items that worked fine. When I clicked on a news item, it sent an index to fetch the next API post. However, every time I clicked on any item, it always took me to the same page. What’s even more strange is that when I tried to modify the API ID, it still directed me to that same page. Initially, I thought there was no issue with the previous page, but the problem lies here. I tried to get the index as expected, and even the URL link matched my expectations. However, the problem arose when the HTTP GET response body changed afterward. I use adapter and view method in this page.
Description Adapter
import 'package:flutter/material.dart';
import 'package:pukulenam/PartView/DescriptionView.dart';
import '../NavBar/BottomBar.dart';
import '../NavBar/TabIconData.dart';
import '../Themes/MainThemes.dart';
class DescriptionAdapter extends StatefulWidget {
final int index;
const DescriptionAdapter({Key? key, required this.index}) : super(key: key);
@override
_DescriptionAdapterState createState() => _DescriptionAdapterState();
}
class _DescriptionAdapterState extends State<DescriptionAdapter>
with TickerProviderStateMixin {
late AnimationController animationController;
late int index;
List<TabIconData> tabIconsList = TabIconData.tabIconsList;
@override
void initState() {
super.initState();
tabIconsList.forEach((TabIconData tab) {
tab.isSelected = false;
});
index = widget.index;
animationController = AnimationController(
duration: const Duration(milliseconds: 600),
vsync: this,
);
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xFFF2F3F8),
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: <Widget>[
FutureBuilder<bool>(
future: getData(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return DescriptionView(
animationController: animationController,
activityIndex: index,
);
}
},
),
bottomBar(),
],
),
),
);
}
Future<bool> getData() async {
await Future<dynamic>.delayed(const Duration(milliseconds: 200));
return true;
}
Widget bottomBar() {
return Column(
children: <Widget>[
const Expanded(
child: SizedBox(),
),
BottomBarView(
tabIconsList: tabIconsList,
addClick: () {},
changeIndex: (int index) {
if (index == 0) {
animationController?.reverse().then<dynamic>((data) {
Navigator.of(context).pop();
});
} else if (index == 1 || index == 3) {
animationController?.reverse().then<dynamic>((data) {
if (!mounted) {
return setState(() {});
}
setState(() {});
});
}
},
),
],
);
}
}
Description View
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:pukulenam/Models/NewsData.dart';
import '../Themes/MainThemes.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
class DescriptionView extends StatefulWidget {
const DescriptionView({
Key? key,
required this.animationController,
required this.activityIndex,
}) : super(key: key);
final AnimationController? animationController;
final int activityIndex;
@override
_DescriptionViewState createState() => _DescriptionViewState();
}
class _DescriptionViewState extends State<DescriptionView>
with TickerProviderStateMixin {
late AnimationController animationController;
late Future<List<NewsData>> selectedActivity;
late int activityIndex;
@override
void initState() {
super.initState();
activityIndex = widget.activityIndex;
animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 2000),
);
animationController.forward();
selectedActivity = fetchNewsList();
}
Future<List<NewsData>> fetchNewsList() async {
return http
.get(
Uri.parse('https://pukulenam.id/wp-json/wp/v2/posts?id=${activityIndex}'),
headers: {
'Authorization':
},
)
.then((response) {
if (response.statusCode == 200) {
print('activity indexnya padahal ${activityIndex}');
List<NewsData> newsList = [];
List<dynamic> data = json.decode(response.body);
data.forEach((item) {
NewsData news = NewsData.fromJson(item);
newsList.add(news);
});
return newsList;
} else {
throw Exception('Failed to load news. Status Code: ${response.statusCode}');
}
}).catchError((error) {
throw Exception('Failed to load news. Error: $error');
});
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
Widget getAppBarUI(String name) {
return Column(
children: <Widget>[
AnimatedBuilder(
animation: animationController,
builder: (BuildContext context, Widget? child) {
return FadeTransition(
opacity: animationController,
child: Transform(
transform: Matrix4.translationValues(
0.0,
30 * (1.0 - animationController.value),
0.0,
),
child: Container(
decoration: BoxDecoration(
color: MainAppTheme.white.withOpacity(1.0),
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(32.0),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: MainAppTheme.grey.withOpacity(0.4),
offset: const Offset(1.1, 1.1),
blurRadius: 10.0,
),
],
),
child: Column(
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).padding.top,
),
Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipOval(
child: Container(
width: 60,
height: 60,
color: Colors.grey,
child: Image(
image: AssetImage('assets/images/sayang.jpg'), // Default image
fit: BoxFit.cover,
),
),
),
),
SizedBox(width: 10), // Spacer
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
],
),
),
IconButton(
onPressed: () {
// Handle share button press
},
icon: Icon(Icons.share),
color: Colors.black,
),
],
),
),
],
),
),
),
);
},
),
],
);
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 80.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
FutureBuilder<List<NewsData>>(
future: selectedActivity,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Failed to load news'));
} else if (snapshot.hasData && snapshot.data!.isNotEmpty) {
final newsData = snapshot.data![0];
var document = HtmlWidget(newsData.content!);
return Column(
children: [
getAppBarUI(newsData.author ?? 'Unknown Author'),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (newsData.imageUrl != null)
Container(
height: 200, // Adjust height as needed
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(newsData.imageUrl!),
fit: BoxFit.cover,
),
),
),
SizedBox(height: 10),
if (newsData.title != null)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
newsData.title!,
textAlign: TextAlign.justify,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
SizedBox(height: 30),
if (newsData.content != null)
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: document,
),
],
),
),
],
);
} else {
return Center(child: Text('No data available'));
}
},
),
],
),
),
);
}
}
and the funny console log i got
I/flutter (28268): 2422 I/flutter (28268): test I/flutter (28268): linknya seharusnya ini https://pukulenam.id/wp-json/wp/v2/posts?id=2422 I/flutter (28268): https://pukulenam.id/wp-json/wp/v2/posts?id=2422 I/flutter (28268): [{"id":2447,"
see it has different id in response body
I expecting the response body has same as the api id they got from before page