Retrive data from laravel API to flutter app - laravel-5

I have created a connection between my flutter app and laravel api, but i don't know why i can't get some data (image) from the back-end to be display on the app. I'm running the laravel api on a local server and flutter app on an android emulator.
Here's my code...
From Laravel api - CategoriesResource.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class CategoriesResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'categoryName' => $this->categoryName,
'category_image' => 'http://127.0.0.1:8000/api/v1/public/category_images/'.$this->category_image
];
}
}
My flutter code ...
import 'package:flutter/material.dart';
import 'package:restaurant_app/models/category.dart';
class HomeCategoriesGet extends StatefulWidget {
final List<Category> categoryList;
final int categoryId;
final String categoryName;
final String categoryImage;
#override
HomeCategoriesGet(
{this.categoryList,
this.categoryId,
this.categoryName,
this.categoryImage});
_HomeCategoriesGetState createState() => _HomeCategoriesGetState();
}
class _HomeCategoriesGetState extends State<HomeCategoriesGet> {
#override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: this.widget.categoryList.length,
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
Image.network(
// This is where i am trying to get the image
this.widget.categoryList[index].catImage,
width: 190.0,
height: 160.0,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(this.widget.categoryList[index].name),
),
],
),
);
},
);
}
}
Error
════════════════════════════ Exception caught by image resource service ════════════════════════════
The following SocketException was thrown resolving an image codec:
OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 48849
When the exception was thrown, this was the stack
#0 NetworkImage._loadAsync
package:flutter/…/painting/_network_image_io.dart:84
<asynchronous suspension>
#1 NetworkImage.load
package:flutter/…/painting/_network_image_io.dart:48
#2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure>
package:flutter/…/painting/image_provider.dart:316
#3 ImageCache.putIfAbsent
package:flutter/…/painting/image_cache.dart:160
...
Image provider: NetworkImage("http://127.0.0.1:8000/api/v1/public/category_images/starters_1578944124.jpeg", scale: 1.0)
Image key: NetworkImage("http://127.0.0.1:8000/api/v1/public/category_images/starters_1578944124.jpeg", scale: 1.0)
Please here's my Repository.dart file
import 'package:http/http.dart' as http;
// Create connection with Back-end (laravel)
class Repository {
// connection with emulator
String _baseUrl = 'http://10.0.2.2:8000/api';
httpGet(String api) async {
return await http.get(_baseUrl + "/" + api);
}
httpGetById(String api, id) async {
return await http.get(_baseUrl + "/" + api + "/" + id.toString());
}
}
Please help me solve this. Thanks

To solve this:
first run php artisan serve --host 0.0.0.0
Then get your local IP by typing the following command on the terminal :
for Windows : ipcofing
Linux or Mac : ifconfig
In my case it's: IPv4 Address . . . . . . . . . . : 192.168.1.122
Finally:
`
class Repository {
// connection with emulator
String _baseUrl = 'http://192.168.1.122:8000/api';
....
}
`

remove the /api in the baseUrl variable

Related

How to include git ignored file in github actions with flutter?

In my project I have some JSON files that are used to get environment configurations at runtime through an entrypoint to the main dart file, they hold sensitive data so I put their containing folder in the .gitignore file.
I have written a test that passes when I run it locally, but fails when triggered by the github action because of this error:
The following assertion was thrown running a test:
Unable to load asset: assets/config/dev.json
Is there a way to somehow inject this JSON while performing the action? Any help is much appreciated, my only concern is having private data not stored in the github repository and having the action pass.
This is my code:
custom entry point
dev_run.dart
import 'package:gitgo/main.dart' as App;
void main() {
App.main('dev');
}
The class that has access to the JSON:
EnvironmentConfig
import 'dart:convert';
import 'package:flutter/services.dart';
class EnvironmentConfig {
final Map<String, String> environment;
EnvironmentConfig({this.environment});
static Future<EnvironmentConfig> forEnvironment(String env) async {
env = env ?? 'dev';
final contents = await rootBundle.loadString(
'assets/config/$env.json',
);
final Map<String, String> json = Map.castFrom(jsonDecode(contents));
return EnvironmentConfig(environment: json);
}
}
Dependencies
pubspec.yaml
name: gitgo
description: Git on the go.
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
http: ^0.12.1
oauth2: ^1.6.1
url_launcher: ^5.4.10
flutter_config: ^1.0.8
gql: ^0.12.3
gql_exec: ^0.2.4
gql_link: ^0.3.0
gql_http_link: ^0.3.2
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.11.1
gql_build: ^0.0.11
pedantic: ^1.9.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/config/
Main file
main.dart
import 'package:flutter/material.dart';
import 'package:gitgo/inbound/configuration/config_environment_widget.dart';
import 'package:gitgo/outbound/api/viewer.req.gql.dart';
import 'package:gql_exec/gql_exec.dart';
import 'package:gql_link/gql_link.dart';
import 'package:gitgo/outbound/api/viewer.data.gql.dart';
import 'package:gitgo/outbound/auth.dart';
import 'package:gql_http_link/gql_http_link.dart';
import 'inbound/configuration/environment_config.dart';
Future main(String env) async {
WidgetsFlutterBinding.ensureInitialized();
final config = await EnvironmentConfig.forEnvironment(env);
print("starting app in $env mode");
runApp(MyApp(config));
}
class MyApp extends StatelessWidget {
final EnvironmentConfig config;
MyApp(this.config);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'GitGo Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: EnvironmentConfigWidget(
config: config, child: MyHomePage(title: 'GitGo Demo Home Page')),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState(title: "GitHub login");
}
class _MyHomePageState extends State<MyHomePage> {
_MyHomePageState({this.title});
EnvironmentConfigWidget environmentConfigWidget;
String title;
#override
Widget build(BuildContext context) {
return GithubLoginWidget(
builder: (context, httpClient) {
final link = HttpLink(
'https://api.github.com/graphql',
httpClient: httpClient,
);
return FutureBuilder<$ViewerDetail$viewer>(
future: viewerDetail(link),
builder: (context, snapshot) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Text(
snapshot.hasData
? 'Hello ${snapshot.data?.login}!'
: 'Retrieving viewer login details...',
),
),
);
},
);
},
githubClientId: EnvironmentConfigWidget.of(context)
.config
.environment['githubClientId'],
githubClientSecret: EnvironmentConfigWidget.of(context)
.config
.environment['githubClientSecret'],
githubScopes: EnvironmentConfigWidget.of(context)
.config
.environment['githubScopes']
.split(","));
}
}
Future<$ViewerDetail$viewer> viewerDetail(Link link) async {
var result = await link.request(ViewerDetail((b) => b)).first;
if (result.errors != null && result.errors.isNotEmpty) {
throw QueryException(result.errors);
}
return $ViewerDetail(result.data).viewer;
}
class QueryException implements Exception {
QueryException(this.errors);
List<GraphQLError> errors;
#override
String toString() {
return 'Query Exception: ${errors.map((err) => '$err').join(',')}';
}
}
Failing test in github actions but not locally
widget_dart.test
import 'package:flutter_test/flutter_test.dart';
import 'package:gitgo/inbound/configuration/environment_config.dart';
import 'package:gitgo/main.dart';
void main() {
testWidgets('Smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
var env = await EnvironmentConfig.forEnvironment('dev');
await tester.pumpWidget(MyApp(env));
// Verify that our counter starts at 0.
expect(find.text('Log into Github'), findsOneWidget);
expect(find.text('1'), findsNothing);
});
}
Github action
ci.yml
name: ci
jobs:
ci:
name: ci
runs-on: macos-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Flutter test
uses: subosito/flutter-action#v1.4.0
with:
flutter-version: '1.26.0-17.2.pre'
channel: dev
- run: flutter pub get
- run: flutter analyze
- run: flutter test --no-sound-null-safety
- name : Clean merged branches
run: |
git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done
echo "Finished cleaning"
You can use GitHub Action secrets.
Put your dev.json contents in a secret (for example, DEV_JSON_CONTENTS), and write it to the correct location with a command.
- name: Write dev.json
run: echo '{{ secrets.DEV_JSON_CONTENTS }}' > assets/config/dev.json
The proposed solution didn't work for me, but as hacker1024 pointed out:
Put your dev.json contents in a secret (for example, DEV_JSON_CONTENTS), and write it to the correct location with a command.
So what worked in my case, was adding the github secret DEV_JSON with the contents of the original file, then
I modified the workflow file by adding the secret as an env variable in the workflow:
env:
DEV_JSON : ${{ secrets.DEV_JSON }}
And I added this step to write the contents to a new file:
- name: Write dev.json
run: |
touch assets/config/dev.json
echo $DEV_JSON >> assets/config/dev.json
cat assets/config/dev.json

play audio in flutter

i made one demo in flutter its work fine in Android, iphone, linux desktop and mac desktop but in windows desktop it give me error
[ERROR:c:\b\s\w\ir\cache\builder\src\flutter\lib\ui\ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method stop on channel xyz.luan/audioplayers)
can any one help me to resolve this error?
in this demo i used audioplayers 0.14.2 to play local audio file in flutter.
here is my code:-
void main() {
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
runApp(new MaterialApp(home: new ExampleApp()));
}
class ExampleApp extends StatefulWidget {
#override
_ExampleAppState createState() => new _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
AudioPlayer advancedPlayer;
AudioCache audioCache;
#override
void initState() {
super.initState();
initPlayer();
}
void initPlayer() {
advancedPlayer = new AudioPlayer();
audioCache = new AudioCache(fixedPlayer: advancedPlayer);
}
void playfirst() {
audioCache.play('audio1.mp3');
}
void stop() {
advancedPlayer.stop();
}
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 1,
child: Scaffold(
appBar: AppBar(
title: Text('Audio file demo'),
),
body: Center(
child: Column(
children: <Widget>[
ButtonTheme(
minWidth: 48.0,
child: RaisedButton(child: Text("play"), onPressed: playfirst)),
ButtonTheme(
minWidth: 48.0,
child: RaisedButton(child: Text("stop"), onPressed: stop))
],
),
)
),
);
}
}
can any one help me to resolve this error?
The only way to resolve the error would be to write an implementation of that plugin for Windows; the reason it's telling you that the plugin is missing is that the plugin doesn't have Windows support (which you can tell because there's no windows folder in the plugin repository, or windows: entry in the pubspec.yaml).
its work fine in [...] linux desktop
That's surprising, given that the plugin you are using doesn't support Linux either.
It's unlikely that you're going to find a plugin that has Windows support at the moment given that the official documentation currently says:
Note: Windows and Linux plugin APIs and tooling are not yet stable, so any plugin written now will need frequent updates for breaking changes. Because of this, publishing Windows and/or Linux plugins to pub.dev at this stage is strongly discouraged.

Flutter - Streaming and Caching videos

I'm developing an application in flutter which is showing videos in a list (like Instagram). Videos must be streamed so I can't download them first and then play them.
I want to cache them while they are being streamed. I've seen CacheManager class but it will download the whole file and then pass it to video player to play it.
How can I implement a cache manager to show the video file while it is being downloaded to cache folder?
I might be writing this a bit late but just in case anybody looking for a solution soon.
By the moment the official video_player plugin doesn't support video caching over network yet.
But fortunately, there been some attempts by contributors to add caching feature to the video_player plugin
You can track updates and find another PRs here: https://github.com/flutter/flutter/issues/28094
Replace video_player in your pubspec.yaml with
video_player:
git:
url: https://github.com/sanekyy/plugins.git
ref: caching
path: packages/video_player/video_player
In case of you are using chewie or other libraries that depend on video_player plugin add:
dependency_overrides:
video_player:
git:
url: https://github.com/sanekyy/plugins.git
ref: caching
path: packages/video_player/video_player
And now to cache the video pass useCache : true to
_videoPlayerController = VideoPlayerController.network(videoURL, useCache: true);
By default both maxFileSize and maxCacheSize is set to 10 * 1024 * 1024 bytes.
To adjust the cache size:
VideoPlayerController.setCacheSize(100 * 1024 * 1024, 200 * 1024 * 1024);
Another Solution:
Is to stream the video normally using the official plugin and to cache the video file using flutter_cache_manager simultaneously.
But this will lead to fetch the video twice the first time (Once for streaming through the video_player, Another for downloading the video through the cachemanager)
Here how the scenario would goes:
1- Check with flutter_cache_manager if the video_url is already downloaded and cached.
2- if the video is cached, pass the file path to video_player VideoPlayerController.file(path), if not download the file using cachemanager and stream the video using VideoPlayerController.network(videoURL) (at this moment video is being fetched twice... by videoplayer and cachemanager).
Visit https://github.com/elgsylvain85/cachedflickvideoplayer.git
It is a video player for flutter. It combines both: The flick_video_player plugin for the base architecture, own set of UI and The cached_video_player plugin for cache supporting.
In your pubspec.yaml file :
cachedflickvideoplayer:
git:
url: https://github.com/elgsylvain85/cachedflickvideoplayer.git
a demo of code :
import 'package:cached_video_player/cached_video_player.dart';
import 'package:cachedflickvideoplayer/cachedflickvideoplayer.dart';
import 'package:cachedflickvideoplayer/controls/flick_landscape_controls.dart';
import 'package:cachedflickvideoplayer/controls/flick_video_with_controls.dart';
import 'package:cachedflickvideoplayer/manager/flick_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class ViewPage extends StatefulWidget {
FlickManager flickManager;
ViewPage() {
flickManager = initVideo();
}
#override
_ViewPageState createState() => _ViewPageState();
FlickManager initVideo() {
return FlickManager(
cachedVideoPlayerController:
CachedVideoPlayerController.network('https://media.istockphoto.com/videos/blurred-motion-of-people-in-restaurant-blur-background-video-id1190840021'),
);
}
}
class _ViewPageState extends State<ViewPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Card(
margin: const EdgeInsets.fromLTRB(20, 20, 20, 100),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: const EdgeInsets.all(8),
height: 300,
child: FlickVideoPlayer(
flickManager: widget.flickManager,
flickVideoWithControlsFullscreen:
FlickVideoWithControls(
videoFit: BoxFit.contain,
controls: FlickLandscapeControls(),
),
)),,
]),
),
],
));
}
#override
void dispose() {
super.dispose();
widget.flickManager.dispose();
}
}
You can mix video_player with flutter_cache_manager.
/// Get a file from the cache or download and insert it into the memory.
final url = 'link-to-the-file';
final file = await DefaultCacheManager().getSingleFile(url, key: url);
_videoController = VideoPlayerController.file(file)
..initialize().then(
(_) => initialize(),
);
As the result, if the manager does not find a file in the cache by url, it will download it, insert it into the memory and return the file object.
This solution is only effective if you work with small sizes video.

How do I pass an Internationalization Object to Child Widgets in Flutter

Just getting started with Flutter/dart, transitioning for PHP, and struggling to figure out how to pass classes into widgets.
I am working on creating my first android and iOS applications using flutter.
I am working with internationalization and everything works fine at my initial build page using the internationalization class I have. However, when passing it on to another widget I get:
NoSuchMethodError: The getter textTitle was called on null.
Receiver: null
tried calling: textTitle
What is the best way of handling this?
Flutter Doctor
[✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D47, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.1)
[✓] Android Studio (version 3.0)
[✓] Connected devices (1 available)
Localization dart
class HnLocalizations{
HnLocalizations(this.locale);
final Locale locale;
static HnLocalizations of(BuildContext context){
return Localizations.of<HnLocalizations>(context, HnLocalizations);
}
static Map<String, Map<String, String>> _localizedValues = {
'en': {
'btnLabelLoginS1': 'Login',
'btnLabelRegisterS1': 'Sign Up'
},
;
String get s1ButtonLabelLogin =>
_localizedValues[locale.languageCode]['btnLabelLoginS1'];
class HnLocalizationsDelegate extends LocalizationsDelegate<HnLocalizations> {
const HnLocalizationsDelegate();
#override
bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode);
#override
Future<HnLocalizations> load(Locale locale) =>
new SynchronousFuture<HnLocalizations>(new HnLocalizations(locale)); //HnLocalizations.load(locale);
#override
bool shouldReload(HnLocalizationsDelegate old) => false;
}
Main Dart
void main() {
runApp(new MaterialApp(
localizationsDelegates: [
const HnLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), /// Americans
const Locale('en', 'GB') /// Brits
],
title: 'HN',
home: new EntryPage(),
));
}
class EntryPage extends StatelessWidget {
final HnColors _hnColors = new HnColors();
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
// !!!**** THIS WORKS AS EXPECTED ****!!!!
title: new Text(HnLocalizations.of(context).s1ButtonLabelLogin),
backgroundColor: _hnColors.transparent(),
elevation: 0.0,
),
backgroundColor: _hnColors.accent(),
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/Background_World.png"),
fit: BoxFit.fitWidth,
),
),
child: new PanelArea(),
)
);
}
}
class PanelArea extends StatelessWidget {
#override
Widget build(BuildContext context) {
HnColors _hnColors = new HnColors();
return new Container(
child: new Center(
child: new Container(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(15.0),
color: _hnColors.transparent()
),
child: new Column(
children: [
new Image.asset('assets/Icon_Intro_login'),
new Text(
// !!!**** ERROR IS HERE ****!!!!
HnLocalizations.of(context).s1M1HeaderTitle,
style: new TextStyle(
color: _haillioColors.white()
),
),
What is the best way of handling this?
Update: Mar 11 2018
I've discovered that if I move all of the code into the main.dart file. All localization works fine. However, when I move my widgets into a separate dart file, the errors come back, even though all of the code is the same.
Update: Mar 12 2018
nicolás-carrasco pointed in the right direction by a reference in this post (Thank you). The problem had to do with imports which is addressed in this post here which ended up being the solution that worked for me. The example is added below in answers.
Nicolás Carrasco pointed towards a solution which was related to what was causing the problem for me by way of link to this post.
In the localization.dart file imports I had:
import 'package:hn/hnLocalization.dart';
While in the main.dart file imports I had:
import 'hnLocalization.dart';
These are not the same thing as described here
Making sure that all files are imported using relative paths vs packages resolved the problem. The distinction is that my files, not the dependencies use relative path. That part stumped at first.
Now my localization.dart file has the following.
import 'hnLocalization.dart'; // <--- Using relative Path
class PanelArea extends StatelessWidget {
#override
Widget build(BuildContext context) { ...
child: new Column(
children: [
new Image.asset('assets/Icon_Intro_login'),
// This Now Works --->
new Text(HnLocalizations.of(context).s1M1HeaderTitle,
]
...
and all is right now.

Error while using #angular compiler in Angular 5 and AOT-Build

i am using the Angular Compiler to compile components in runtime. This Code works fine, but if I want to use AOT-Prerendering the Component wont work, because Angular does not load the Compiler in AOT-Build.
I've read about some Workarounds that wont Work in Angular5+ anymore.
Do you have any solutions for this problem?
Best Regards
export class RuntimeCompilerComponent {
template: string = "";
#ViewChild('dynamicComponent', { read: ViewContainerRef }) container: ViewContainerRef;
constructor(private compiler: Compiler) { }
//Ruft die addComponent Methode auf
createComponent() {
this.addComponent(this.template, null);
}
// Komponente wird dynamisch erzeugt und geladen
// Sollten sich die properties ändern muss ggf. die Changedetection manuell aufgerufen werden.
private addComponent(template: string, properties: any = {}) {
#Component({ template })
class TemplateComponent { }
#NgModule({
imports: [
AppModule,
CommonModule,
ReactiveFormsModule,
FormsModule,
BrowserModule,
], declarations: [TemplateComponent]
})
class TemplateModule { }
const mod = this.compiler.compileModuleAndAllComponentsSync(TemplateModule);
const factory = mod.componentFactories.find((comp) =>
comp.componentType === TemplateComponent
);
const component = this.container.createComponent(factory);
Object.assign(component.instance, properties);
}
}
You can make this work with a few tricks. I ran into the same problem last year, and was able to find a fix. I was using dynamically generated angular components in my style-guide. Here's a working example, which works with AOT compilation in Angular 7:
https://github.com/johncrim/angular-dynamic-styleguide
The README.md for that project provides some additional info on the problems I came up against and how I figured out the fixes.

Resources