Implementing MVP arch in flutter app

class BasePresenter<T>{
T view;
void attachView(T view){
this.view=view;
}
void detachView(){
this.view=null;
}
bool get isViewAttached{
return view!=null;
}
void checkViewAttached(){
if(view==null){
throw new Exception("attached view is null!");
}
}
T getView(){
return view;
}
}
import 'package:flutter/material.dart';void main() => runApp(new MyApp());class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new HomePage(title: 'MVP app'),
);
}
}
import 'package:flutter/material.dart';class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);

@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> { String _text="Loading...";
@override
Widget build(BuildContext context) {

return new Scaffold(
appBar: new AppBar(
title: new Text("MVP app"),
),
body: new Center(
child: new Text(_text),
),
);
}
}
abstract class HomePageView{
onLoadText();
}
import 'package:flutter_mvp_app/data/network/ApiHelper.dart';AppDataManager appDataManager = new AppDataManager();class AppDataManager {
static final AppDataManager _appDataManager = new AppDataManager._internal();
final ApiHelper apiHelper = new ApiHelper();
// SharedPrefsHelper sharedPrefsHelper=new SharedPrefsHelper();
factory AppDataManager() {
return _appDataManager;
}
AppDataManager._internal();
}
import 'dart:async';import 'package:http/http.dart' as http;class ApiHelper {
Future fetchtext() async {
var response =
await http.post("https://www.reweyou.in/fasalapp/fetchtext.php");
return response;
}
}
import 'dart:async';import 'package:flutter_mvp_app/data/AppDataManager.dart';
import 'package:flutter_mvp_app/ui/base/base_presenter.dart';
import 'package:flutter_mvp_app/ui/home_page_view.dart';
import 'package:flutter_mvp_app/utils/network_utils.dart';
class HomePagePresenter extends BasePresenter<HomePageView> {
Future getText() async {
checkViewAttached();
var response = await appDataManager.apiHelper.fetchtext();
if (NetworkUtils.isReqSuccess(response)) {
String text = response.body;
isViewAttached ? getView().onLoadText(text) : null;
} else {
isViewAttached ? getView().onFailLoadText() : null;
}
}
}
import 'package:http/http.dart' as http;class NetworkUtils {
static isReqSuccess(http.Response response) {
print(response.body);
if (response.statusCode < 200 || response.statusCode >= 300) {
return false;
} else {
return true;
}
}
}
import 'package:flutter/material.dart';
import 'package:flutter_mvp_app/ui/home_page_presenter.dart';
import 'package:flutter_mvp_app/ui/home_page_view.dart';
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> implements HomePageView {
String _text="Loading...";
HomePagePresenter _presenter;
@override
void initState() {
// TODO: implement initState
super.initState();
_presenter=new HomePagePresenter();
_presenter.attachView(this);
_presenter.getText();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("MVP app"),
),
body: new Center(child: new Text(_text,style: new TextStyle(fontSize: 18.0),),
);
}
@override
onLoadText(String text) {
setState(() {
_text=text;
});
}
@override
onFailLoadText() {
setState(() {
_text="Failed";
});
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ayush P Gupta

Ayush P Gupta

336 Followers

Flutter | Vue | IIT Roorkee | Physics Educator at Unacademy | Artist | Flute Player | Love for machinary, bikes, tools | DIY person